Wednesday, July 22, 2009

bash: check size of all folders in a given directory

To list all folders in a given path

this can be usedfor f in ./* ; do if [ -d "$f" ]; then echo $f ; fi done;

To list sizes of all folders in a given path

To check the size of all folders in the current directory the above can be modified to use du -sh "$f" command: for f in * ; do if [ -d "$f" ]; then du -sh "$f" ; fi done;The command goes in a loop on every file in a current dir and if a given file is a directory, than the size is calculated.

To sort by folder size

for f in * ; do if [ -d "$f" ]; then du -s "$f" ; fi done | sort -nNote that in du command there is no -h option!

2 comments:

  1. Here's what I use to sort folders by size (human-readable):

    du -k --max-depth=1 | sort -n | cut -f2 | xargs -d '\n' du -sh

    ReplyDelete