Or you could do the same stat() call in Perl if you prefer
Main Topics
Browse All TopicsHi, I'm trying to get the size of a file using a unix shell. . . . . is there a specific command to get this info?
Thank's
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
HamdyHassan.
No need to waste loads of variables that never get used.
print "Size of filename in bytes = ",(stat shift)[7],"\n";
Perl or C certainly is the most reliable way of doing it as any solution that relies on the output from ls is not going to be very portable unless a whole range of checks are included.
When at the shell prompt all you need to do is add the -l command to the ls command.
$ ls -l
This will display your current directory contents in a long listing style, complete with the number of 'blocks' used by each file.
$ ls -l filename
This will display just 'filename' in a long listing.
There may be different flags between versions of shells, to do examples like:
$ ls -lh To get the long listing in a 'human readable' format
or
$ ls -lk To get the long listing with the size in kilobytes.
Check your manual for ls for more flags that may help you
$ man ls
You may also be interested in the 'du' command for finding the total size of directories.
$ man du
I hope this is appropriate!
Ya thanks tintin. dunno what I was thinking.
You can always do
$du -sh <<filename>>
To extend the scope, the following can be used to process sizes of every file in a directory
If you are trying to do this inside a script for any directory,
try running the following script in that directory
#!/usr/bin/ksh
for filnam in `ls -lrt | sed -e 's/ */|/g'`
do
file_size=`echo ${filnam} | awk -F'|' '{print $5}'`
done
alternatively, u can specify ls -lrt <<target_directory>> to do this in that particular directory
The field file_size can be stored or manipulated
Business Accounts
Answer for Membership
by: dtkernsPosted on 2004-04-06 at 09:29:29ID: 10767405
size=`ls -l file | awk '{print $5 }'`
if your doing a LOT of these it might be worth writing a 5 line C program that calls the stat(2) system call on argv1...argvn