Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get file size of a file 2

Status
Not open for further replies.

philipose

Programmer
Dec 24, 2003
137
US
unix gurus,
i wanted to get the filesize of a bunch of files (individually or collectively). i can use the cut piped to an ls or a find to get the filesize, but wanted to know if there were any direct functions to get the filesize of a file.
Please let me know about your thoughts.
Thanks in advance
philipose
 
You are far better off with something like perl which can call stat directly. For example the perl script
Code:
#!/bin/perl -w
use strict;
my $allfiles = 0;
foreach ( @ARGV )
  {
  my $fsize = (stat)[7];
  print "The size of $_ is $fsize bytes\n";
  $allfiles += $fsize;
  }
print "The size of all files is $allfiles\n";
I called this fsize.pl. It just needs a list of files passed to it. For example, in my home directory
Code:
g03201# ./fsize.pl *.pl
The size of PZ2189.pl is 1129 bytes
The size of add_to_group.pl is 739 bytes
The size of col.pl is 194 bytes
The size of crypt.pl is 404 bytes
The size of do_users.pl is 837 bytes
The size of fsize.pl is 221 bytes
The size of go.pl is 314 bytes
The size of mklist.pl is 819 bytes
The size of mytest.pl is 1234 bytes
The size of rsh.pl is 386 bytes
The size of slices.pl is 145 bytes
The size of test.pl is 126 bytes
The size of test_date.pl is 121 bytes
The size of ws.pl is 321 bytes
The size of xmlparser.pl is 140 bytes
The size of all files is 7259
And with AIX perl comes built in as default. I know it may not be a skill you already have but it's one worth learning.

Columb Healy
 
Columb Healy,
Great tip. Works like a charm. I had a question. Is there a way to use the perl stat function from a kshell program directly? I am not very familiar with perl commands.
philipose
 
Not really. Whilst I'm a big ksh fan I strongly recommend that you add perl to your toolbox. The O'Reilly books are a great place to start. There are also many free web resources starting with CPAN. Within hours you'll be writing short scripts which overcome some of the areas where ksh can't providesimple answers.

The perl forum is also a useful resource.

Ceci n'est pas un signature
Columb Healy
 
Yes really:

This is from within a ksh:

# perl -e 'printf "%d\n", (stat("file.tst"))[7]'
16000

It's kind of tricky to use shell variables, but can be done also:

# FN=file.tst
# perl -e "printf \"%d\n\", (stat(\"${FN}\"))[7]"
16000
# SZ=$(perl -e "printf \"%d\n\", (stat(\"${FN}\"))[7]")
# echo $SZ
16000

If your ksh script is already there, no need to convert it to perl in order to use stat(). But I'd clearly document the use of this perl stat builtin in your script.



HTH,

p5wizard
 
p5wizard,
this is even better. thanks to both p5wizard and columb
regards
philipose
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top