Solved

file size from a unix shell

Posted on 2004-04-06
16
23,404 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

10 Experts available now in Live!

Get 1:1 Help Now