• Using Sort to List Directories by Size

    If you manage a UNIX system with a large number of directories that vary in size, chances are that you’ve needed to figure out which ones are using up the most disk space. Of course if the directories are user accounts, the best way to do this is to enable quotas and use the “repquota” command. If you just have a bunch of directories, however, you can easily figure out which ones are largest by giving the correct arguments to “du” and “sort”. Here is how:

    du -sk * | sort +0nr

    This will display the size of all directories and sort them from largest to smallest. If you want to sort them from smallest to largest, simply remove the “r”.

    du -sk * | sort +0n

    If you have nested directories, you will need to incorporate foreach to recurse through and get all the directory names.

    This entry was posted on Monday, November 27th, 2006 at 12:37 pm and is filed under Data and Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 9 Comments

    Take a look at some of the responses we have had to this article.

    1. Slack
      Nov 19th
      Reply

      Useful command, but it doesn’t show the size of hidden directories. How could I get it to show them?

    2. Nov 20th
      Reply

      Good question… This should do it.

      % du -sk .[a-z]* | sort +0nr

    3. AM
      Jan 14th
      Reply

      Thank you! This is very, very helpful.

    4. thanks, i needed that. although i wish there was a way to show the sizes of directories and files in the same list. like why doesn’t the ls command have a flag to show recursive directory sizes?
      (::)

    5. Sort on size the dirs only

      $ du –max-depth=1 . | sort -n -r

    6. Aug 20th
      Reply

      Very useful command , thanks for sharing

    7. Sep 10th
      Reply

      Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

    8. sundar
      Sep 12th
      Reply

      Sort directories by size and print it in human readable format:

      du -k –max-depth=1 | sort -n | awk ‘{printf(“%dMB\t%-10s\n”, int($1 / 1024 + 0.5), $2)}’

    9. Useful command many thanks,saved me some time trying to find directory sizes

  • Leave a Reply

    Let us know what you thought.

  • Name(required):

    Email(required):

    Website:

    Message: