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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-18-2005, 07:28 PM
|
#1
|
LQ Newbie
Registered: Nov 2005
Location: Australia
Posts: 18
Rep:
|
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
|
|
|
11-18-2005, 07:44 PM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Is a teacher asking? If so - keep searching :}
Cheers,
Tink
|
|
|
11-18-2005, 07:48 PM
|
#3
|
LQ Newbie
Registered: Nov 2005
Location: Australia
Posts: 18
Original Poster
Rep:
|
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 08:06 PM.
|
|
|
11-18-2005, 08:17 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
ls -l filename|awk '{print $5}'
or
du -sb filename
Cheers,
Tink
Last edited by Tinkster; 11-18-2005 at 08:19 PM.
|
|
|
11-18-2005, 08:34 PM
|
#5
|
LQ Newbie
Registered: Nov 2005
Location: Australia
Posts: 18
Original Poster
Rep:
|
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
|
|
|
11-18-2005, 11:00 PM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
04-25-2006, 11:58 PM
|
#7
|
Member
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157
Rep:
|
thanks Tinkster. that's what I need.
|
|
|
04-29-2006, 03:21 PM
|
#8
|
Member
Registered: May 2005
Location: kerala,india
Distribution: UBUNTU
Posts: 94
Rep:
|
good stuffs
|
|
|
04-29-2006, 04:59 PM
|
#9
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep:
|
Yet another way:
Code:
stat -c %s file.txt
|
|
|
All times are GMT -5. The time now is 06:39 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|