Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

file size from a unix shell

Posted on 2004-04-06
16
Medium Priority
?
23,420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +5
16 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

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

Industry Leaders: 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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

670 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