LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2005, 06:28 PM   #1
harrylmh
LQ Newbie
 
Registered: Nov 2005
Location: Australia
Posts: 18

Rep: Reputation: 0
Question Finding the file size


Hi,
I'm asked to make a shell script that stores the total filesize of all the files in a given directory into a variable. Does any one have any clue on how to do this?

regards
 
Old 11-18-2005, 06:44 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Is a teacher asking? If so - keep searching :}


Cheers,
Tink
 
Old 11-18-2005, 06:48 PM   #3
harrylmh
LQ Newbie
 
Registered: Nov 2005
Location: Australia
Posts: 18

Original Poster
Rep: Reputation: 0
not really. How do find the file size for a file only? like var=`filesize yyy.exe` or some thing like that

Last edited by harrylmh; 11-18-2005 at 07:06 PM.
 
Old 11-18-2005, 07:17 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
ls -l filename|awk '{print $5}'

or

du -sb filename

Cheers,
Tink

Last edited by Tinkster; 11-18-2005 at 07:19 PM.
 
Old 11-18-2005, 07:34 PM   #5
harrylmh
LQ Newbie
 
Registered: Nov 2005
Location: Australia
Posts: 18

Original Poster
Rep: Reputation: 0
ah, that's pretty short. I actually used ls -l for each file, then use a for loop to print out the 5th line. The concept is the same, but the awk method is obviously a lot shorter!

thanks
 
Old 11-18-2005, 10:00 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Welcome!

The advantage of the awk method is that it's next to trivial
to calculate the size of all the files.

Code:
 find -ls | awk '$11 != "." {total+=$7;printf("%12d\t", $7);for(i=11;i<=NF;i++){printf("%s", $i)}printf("\n")}END{print "Total:  ", total}'

Cheers,
Tink
 
Old 04-25-2006, 10:58 PM   #7
zamri
Member
 
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157

Rep: Reputation: 30
thanks Tinkster. that's what I need.
 
Old 04-29-2006, 02:21 PM   #8
sajjadc
Member
 
Registered: May 2005
Location: kerala,india
Distribution: UBUNTU
Posts: 94

Rep: Reputation: 15
good stuffs
 
Old 04-29-2006, 03:59 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Yet another way:
Code:
stat -c %s file.txt
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding installed raw disks and size looseCannon Solaris / OpenSolaris 5 02-23-2005 01:26 PM
Finding File Size cmfarley19 Linux - General 1 01-24-2005 08:36 AM
finding size of total files in a directory blackzone Linux - Newbie 3 01-07-2005 03:01 AM
Finding files via their size ivanatora Linux - General 1 09-08-2004 08:23 AM
Finding size of a directory chrisk5527 Linux - General 2 12-30-2003 08:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration