?
Solved

file size from a unix shell

Posted on 2004-04-06
16
Medium Priority
?
23,423 Views
Last Modified: 2013-12-26
Hi, 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
0
Comment
Question by:jriverag
  • 4
  • 3
  • 2
  • +5
14 Comments
 
LVL 3

Accepted Solution

by:
dtkerns earned 128 total points
ID: 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
0
 
LVL 14

Assisted Solution

by:chris_calabrese
chris_calabrese earned 124 total points
ID: 10767879
Or you could do the same stat() call in Perl if you prefer
0
 
LVL 9

Assisted Solution

by:Alf666
Alf666 earned 124 total points
ID: 10768117
Most Linuxes provide a very nice stat command :

stat -c %s <file>

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:HamdyHassan
ID: 10769251
I agree with chris_calabrese

Here is example

$ cat get_size.pl
#!/export/vol/bin/perl
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat $ARGV[0];
print "Size of $filename in bytes = $size \n";


$ get_size.pl get_size.pl
Size of  in bytes = 217
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10771162
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.

0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 124 total points
ID: 10771425
perl -e '$f=shift;print"Size of $f in bytes = ",-s$f,"\n"' file
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10771440
Doh.  forgot about -s
0
 
LVL 85

Expert Comment

by:ozo
ID: 10771447
perl -e '$_shift;print"Size of $_ in bytes = ",-s,"\n"' filename
0
 
LVL 85

Expert Comment

by:ozo
ID: 10771448
perl -e '$_=shift;print"Size of $_ in bytes = ",-s,"\n"'
0
 
LVL 1

Expert Comment

by:suzer
ID: 11218910
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!

0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 11295063
ls -lrt | nawk `{print $5}`

Remember, the single quotes used are the one which u will find to the left of '1' key on your keyboard. These are the quotes used to return output of a command
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11311231
manav_mathur.

Your "solution" is incorrect.

You don't use backquotes in your example, you need single or double quotes.

Also note that the fifth field from "ls -l" output is not necessarily the size.
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 11314538
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
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11320190
du -sh is not portable.

Did you read ahoffmann's last comments?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question