LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Finding the file size (https://www.linuxquestions.org/questions/programming-9/finding-the-file-size-384483/)

harrylmh 11-18-2005 06:28 PM

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

Tinkster 11-18-2005 06:44 PM

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


Cheers,
Tink

harrylmh 11-18-2005 06:48 PM

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

Tinkster 11-18-2005 07:17 PM

ls -l filename|awk '{print $5}'

or

du -sb filename

Cheers,
Tink

harrylmh 11-18-2005 07:34 PM

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

Tinkster 11-18-2005 10:00 PM

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

zamri 04-25-2006 10:58 PM

thanks Tinkster. that's what I need.

sajjadc 04-29-2006 02:21 PM

good stuffs

Hko 04-29-2006 03:59 PM

Yet another way:
Code:

stat -c %s file.txt


All times are GMT -5. The time now is 02:38 AM.