Solved

file size from a unix shell

Posted on 2004-04-06
16
23,407 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
16 Comments
 
LVL 3

Accepted Solution

by:
dtkerns earned 32 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 31 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 31 total points
ID: 10768117
Most Linuxes provide a very nice stat command :

stat -c %s <file>

0
 
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 84

Assisted Solution

by:ozo
ozo earned 31 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 157
unable to delete all specified values regedit 38 283
Hibernate methods 2 68
Annoying "thing" blocks my view 4 83
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now