Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 20 Sep 2018, 20:25
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to find out the size of a file?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [11 Posts]  
Author Message
ITSMERSH


Joined: 02 May 2018
Posts: 310

PostPosted: Thu 21 Jun 2018, 00:39    Post subject:  How to find out the size of a file?
Subject description: Want to do it in a shell script...
 

Hi.

Title and description says it all.

Any hints?

Thanks

_________________
RSH
Back to top
View user's profile Send private message 
dancytron

Joined: 18 Jul 2012
Posts: 1124

PostPosted: Thu 21 Jun 2018, 00:47    Post subject:  

Fred has a script in Debian Dog that does it as a right click action. Should be in there someplace. it is /usr/local/bin/howbig

Code:

#!/bin/bash

FILES="$@"
printf '%s\n' "$@" > /tmp/howbig

BASENAMES=$(while read f; do echo "$(basename "$f")"; done <<< "$(cat /tmp/howbig)")

DIRNAME="$(dirname "$(cat /tmp/howbig | head -1)")"

IFS=$'\n'

cd "$DIRNAME"
du -chs --apparent-size $BASENAMES | sort -h  | yad --title="How Big?" --width=400 --height=400 --text-info --tail --center
rm -f /tmp/howbig

exit 0
Back to top
View user's profile Send private message 
ITSMERSH


Joined: 02 May 2018
Posts: 310

PostPosted: Thu 21 Jun 2018, 01:57    Post subject:  

Thanks.

I'm using:
Code:
FSIZE1=`du -c -h --apparent-size "$PRJ"`
FSIZE=`echo $FSIZE1 | cut -d " " -f1`

to get the size of a single file, instead of all files in a directory.

_________________
RSH
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1617
Location: Japan

PostPosted: Thu 21 Jun 2018, 02:57    Post subject:  

ITSMERSH wrote:
Thanks.

I'm using:
Code:
FSIZE1=`du -c -h --apparent-size "$PRJ"`
FSIZE=`echo $FSIZE1 | cut -d " " -f1`

to get the size of a single file, instead of all files in a directory.


Try
Code:
FSIZE=$(du -h --apparent-size "$PRJ" | cut -f1)
Note that du separates fields by Tab, not by Space. Your code works only because you echo an unquoted $FSIZE1. In this case echo changes Tabs to Spaces.

Slightly faster:
Code:
FSIZE=$(stat -c %s  "$PRJ")
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 842

PostPosted: Thu 21 Jun 2018, 09:48    Post subject:  

Code:
ls -al file.name | cut -d " " -f 5
Back to top
View user's profile Send private message 
ITSMERSH


Joined: 02 May 2018
Posts: 310

PostPosted: Thu 21 Jun 2018, 11:13    Post subject:  

@MochiMoppel, jafadmin,

thanks for the replies.

MochiMoppel wrote:
ITSMERSH wrote:
Thanks.

I'm using:
Code:
FSIZE1=`du -c -h --apparent-size "$PRJ"`
FSIZE=`echo $FSIZE1 | cut -d " " -f1`

to get the size of a single file, instead of all files in a directory.


Try
Code:
FSIZE=$(du -h --apparent-size "$PRJ" | cut -f1)
Note that du separates fields by Tab, not by Space. Your code works only because you echo an unquoted $FSIZE1. In this case echo changes Tabs to Spaces.

Slightly faster:
Code:
FSIZE=$(stat -c %s  "$PRJ")


I was trying and trying and trying
Code:
FSIZE="`du -c -h --apparent-size "$PRJ" | cut -d " " -f1`"

and wondered why it didn't cut everything after the first space.

Didn't know it is separated by tabs... Rolling Eyes

_________________
RSH
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12686
Location: Gatineau (Qc), Canada

PostPosted: Thu 21 Jun 2018, 11:36    Post subject:  

Hi.

With the help of awk:

Size at beginning followed by file name:
Code:
ls -Algo | awk '{ print $3"\t"$NF }'
or
size at end, file name at beginning:
Code:
ls -Algo | awk '{ print $NF"\t"$3 }'

Bye.

_________________
musher0
~~~~~~~~~~
Siempre será canción nueva... (V. Jara, Manifiesto)
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12686
Location: Gatineau (Qc), Canada

PostPosted: Thu 21 Jun 2018, 15:11    Post subject:  

Hello *RSH and all.

You can see file sizes directly in a ROX-Filer window.
Simply change the ROX-Filer's Options, like so:
    -- Open a ROX-Filer window (it does not matter which directory)

    -- Right-click on en empty part of background. Then click on Options in
    the middle of that menu: ROX's Options panel opens.

    -- Click "Display", 2nd from top.

    -- Go to 2nd sub-title there, named "Icons".

    -- Now in the left colum, 2nd line, where is says "Details by Default",
    there is a list menu: click on "Sizes" there, and then "Save". The next
    time that you open ROX-Filer, it will display the size in bytes (if the file
    is small) or Kb's of files next to the name of the files. Only of files, not
    of directories, though.
Please see illustrations.

Other file managers have similar settings that you can set or unset:
mc, wcm, muCommander, xfe, etc.

IHTH.
Result-sizes_setting.jpg
 Description   
 Filesize   97.23 KB
 Viewed   161 Time(s)

Result-sizes_setting.jpg

ROX-Filer-sizes_setting.jpg
 Description   
 Filesize   106.47 KB
 Viewed   161 Time(s)

ROX-Filer-sizes_setting.jpg


_________________
musher0
~~~~~~~~~~
Siempre será canción nueva... (V. Jara, Manifiesto)
Back to top
View user's profile Send private message 
ITSMERSH


Joined: 02 May 2018
Posts: 310

PostPosted: Thu 21 Jun 2018, 17:13    Post subject:  

Quote:
You can see file sizes directly in a ROX-Filer window.

Yes, I know this. Thanks.

Though as mentioned in my opening post I wanted to do this in a shell script, as I need that information to display the file size along with file name and date of last edition of the file in the status bar or in a disabled entry field of a gtkdialog program I'm working on.

_________________
RSH
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 919

PostPosted: Fri 22 Jun 2018, 10:22    Post subject:  

ITSMERSH,

I think "stat" will give you everything you need.

Code:
FN="/root/.bashrc"
bytes_and_last_change="$(stat --printf="%s ""%z" "$FN" | cut -d " " -f1,2)"
echo "$FN ""$bytes_and_last_change"
  /root/.bashrc 1245 2017-02-10


Cheers,
s
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12686
Location: Gatineau (Qc), Canada

PostPosted: Fri 22 Jun 2018, 11:41    Post subject:  

Thanks seaside.

I didn't know one could do that! Smile

Just having fun:
Code:
Info="`stat -c "%n, %s b, %z" .bashrc`";echo "${Info:0:33}"
More general:
Code:
Info="`stat -c "%n, %s b, %z" filename`";echo "${Info:0: -16}"

BFN.

_________________
musher0
~~~~~~~~~~
Siempre será canción nueva... (V. Jara, Manifiesto)
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [11 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0509s ][ Queries: 12 (0.0051s) ][ GZIP on ]