Solved

file size from a unix shell

Posted on 2004-04-06
16
23,410 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 58
Add content to output file 4 67
maxBlock challenge 30 142
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 826
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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