Friday, October 2, 2009

Directory size excluding sub-directories - Linux


Directory '/home/user/work/demo/' contains a few regular files and two directories say "part2"(size=41236 KB) and "libs"(size=20620 KB).

$ du ~/work/demo/
41236 /home/user/work/demo/part2
20620 /home/user/work/demo/libs
87640 /home/user/work/demo/

From Linux/UNIX DU(1) command man page:

-s, --summarize
display only a total for each argument.

So, the following command is going to display the total size of the directory '/home/user/work/demo/'

$ du -s ~/work/demo/
87640 /home/user/work/demo/

Now, if you need to find the size of the '/home/user/work/demo/' directory excluding the size of the sub-directories, there is a command line option with DU(1):

-S, --separate-dirs
do not include size of sub-directories

So

$ du -S ~/work/demo/
41236 /home/user/work/demo/part2
20620 /home/user/work/demo/libs
25784 /home/user/work/demo/

Now,

$ du -S --max-depth=0 ~/work/demo/
25784 /home/user/work/demo/

or

$ du -S ~/work/demo/ | awk 'END {print}'
25784 /home/user/work/demo/

1 comment:

Hari said...

You could as well use the summary option -s instead of --max-depth, so `du -sS ~/work/demo` should work. Also, why use `awk END {print }` when `tail -1` is much simpler?

© Jadu Saikia www.UNIXCL.com