How Big Is That UNIX Directory?

Need to know how much space a directory and its contents are taking up on your UNIX system? Here’s what I use:

du -ks directory

The du command is used to summarize disk usage. Without any flags will show you the usage in blocks for every directory and subdirectory specified. Since the number of blocks varies by operating system we add the -k option to specify that we want the output in kilobytes. In many operating systems you could also use -h for a “human-readable” output with abbreviations like B for bytes, K for kilobytes, M for megabytes and so on.

The -s option lets us gather only the sum of the directory specified. Without the -s flag we would get output on every subdirectory as well as the specified directory.

$ du -k stuff
408560 stuff/patch
104 stuff/scripts
408688 stuff

One other thing that is useful for finding the biggest files and directories where there are a lot to sift through is to use a wildcard to size up multiple directories, then pipe the output of du to the sort command like this:

$ du -ks ./* | sort -n
0 ./sdtvolcheck727
8 ./mpztaWqc
8 ./speckeysd.lock
304 ./dtdbcache_:0
408688 ./stuff

With sort we use the -n option to order things by arithmetic value rather than alphabetic value (making 8 come before 304) so we see the largest things at the bottom.

Try it out. As always check the man pages for more info.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

unix, solaris, linux, sysadmin, system administration, storage, storage administration

6 thoughts on “How Big Is That UNIX Directory?”

  1. M brings up a good point. This is pretty IO intensive and will load down your system if you’re running it on a big directory. Use it carefully when you’re on production systems.

  2. Since you’ve emphasized the easy part in your booktitle, I’d also advice to use the -h parameter. This will decode the large numbers in readable blocks of kilo-, mega- or gigabytes.

  3. Jan,

    See the end of the second paragraph:

    you could also use -h for a “human-readable” output with abbreviations like B for bytes, K for kilobytes, M for megabytes and so on.

    But, if you’re going to use this with the sort -n you’ll want everything in the same size so it sorts properly.

  4. friends,
    as you know, in solaris 8 systems, we dont have df -h (human readable) option available, can you please any way of using like df -h option (Any script would be helpful for me)


  5. Well, to answer your question on a “du -h” in Solaris 8…my answer’s not a script, but

    As long as you don’t have to worry about a production environment, they provide all the GNUness you could ever want on Solaris (any version on either CPU architecture).

Leave a Reply

Your email address will not be published. Required fields are marked *