1752796 Members
5771 Online
108789 Solutions
New Discussion юеВ

Re: File Size

 
SOLVED
Go to solution
Scott McDade
Frequent Advisor

File Size

Is there an easy way to programmatically read file size either from command line or via script or possible Ansci C funciton?

OS -> 11i
Keep it Simple!~
11 REPLIES 11
Dave Olker
HPE Pro
Solution

Re: File Size

Hi Scott,

The stat(2) system call is probably the most common means of getting this information programmatically.

Look at the stat(2) man page:

off_t st_size; /* File size (bytes) */


The st_size field contains the size of the file in bytes.

Regards,

Dave
I work for HPE

[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
James R. Ferguson
Acclaimed Contributor

Re: File Size

Hi Scott:

Perl has access to the 'stat(2)' function. To list the names of files and their size in bytes, you can do:

# perl -le 'die unless @ARGV;print join " = ", $_, (stat($_))[7] for @ARGV' filename

For example:

# perl -le 'die unless @ARGV;print join " = ", $_, (stat($_))[7] for @ARGV' /etc/hosts /etc/services /etc/rc.config.d/netconf

...might return:

/etc/hosts/ = 606
/etc/services = 9946
/etc/rc.config.d/netconf = 3987

Regards!

...JRF...




James R. Ferguson
Acclaimed Contributor

Re: File Size

Hi Scott:

If you want to limit the output only to regular files, amend my original post to:

# perl -le 'die unless @ARGV;for (@ARGV) {print join " = ", $_,(stat(_))[7] if (-f $_)}' /etc/hosts/ /etc/services /etc/rc.config.d

...returns (only):

/etc/hosts/ = 606
/etc/services = 9946

...since '/etc/rc.config.d' is a *directory*. My first variation treats files and directories equally and reports their byte-sizes.

Regards!

...JRF...

A. Clay Stephenson
Acclaimed Contributor

Re: File Size

... and if you already have the file open, the fstat() system call is a little easier because it uses the file descriptor rather than the pathname as the 1st argument.
If it ain't broke, I can fix that.
Ralph Grothe
Honored Contributor

Re: File Size

If you can live with perl
there's even an easier way than to stat().
I wonder why people always forget about
the simple -s test.
E.g. this should print a size sorted list of all files in /tmp
(n.b. usually one would further refine by excluding the single dot references as well as directories etc.)

$ perl -e 'opendir TMP,"/tmp";print map{sprintf"%30s:%10u\n",$_->[0],$_->[1]}sort{$a->[1]<=>$b->[1]}map[$_,-s "/tmp/$_"],readdir TMP;closedir TMP'


Madness, thy name is system administration
inventsekar_1
Respected Contributor

Re: File Size

Is there an easy way to programmatically read file size either from command line or via script:

"wc -c filename" will "Report the number of bytes in each input file".

is this fine?
[ofcourse, u need to cut the result of wc]
Be Tomorrow, Today.
Arturo Galbiati
Esteemed Contributor

Re: File Size

wc -c
HTH,
Art
Steven E. Protter
Exalted Contributor

Re: File Size

Shalom,

Change:
wc -c filename

filename=
SIZE=$(wc -l $filename)
echo $SIZE

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
inventsekar_1
Respected Contributor

Re: File Size

SEP,

filename=
SIZE=$(wc -l $filename)
echo $SIZE

should be
filename=
SIZE=$(wc -c $filename)
echo $SIZE

isnt?

[-l gives the number of lines
-c gives the number of bytes]
Be Tomorrow, Today.