how do you find out how big your directories are... all at once!!

G

gcawood

Guest
Easy! Loop through the results of `ls` and then apply a `du -hs`. See below!!
 


N

Nathan Rich

Guest
Where to begin

-Never parse ls. The results are meant only for human consumption and it's considered bad practices to do so.
-When parsing commands ` ` is deprecated, $() is preferred.
-Because you have not quoted properly, your script will break when applied to a listing where expansion becomes an issue. An obvious example of this is a file name with a space in it.
-The script doesn't strictly show you the size of your directories, it's in fact all files including directories in your current directory.
-Your script doesn't need to parse ls because your shell will already have a built in way to return all the files in your directory: * , which means this could be shortened to for i in *; do du -hs $i; done . However you will still suffer from improper quotes.
-And finally, this command like most linux commands accepts multiple IFS-separated arguments already. Therefore you could shorten this script to simply: du -hs * , and that will NOT suffer from the improper quotes.

In short:
Code:
du -hs *
 



Top