308

Let's say I want to get the size of each directory of a Linux file system. When I use ls -la I don't really get the summarized size of the folders.

If I use df I get the size of each mounted file system but that also doesn't help me. And with du I get the size of each subdirectory and the summary of the whole file system.

But I want to have only the summarized size of each directory within the ROOT folder of the file system. Is there any command to achieve that?

438

This does what you're looking for:

du -sh /*

What this means:

  • -s to give only the total for each command line argument.
  • -h for human-readable suffixes like M for megabytes and G for gigabytes (optional).
  • /* simply expands to all directories (and files) in /.

    Note: dotfiles are not included; run shopt -s dotglob to include those too.

Also useful is sorting by size:

du -sh /* | sort -h

Here:

  • -h ensures that sort interprets the human-readable suffixes correctly.
  • 8
    If you have dot-directories in the root directory, you can use shopt -s dotglob to include them in the count. – Philipp Jul 11 '10 at 21:55
  • 9
    It's very usefull, because it's simple and you can place what path you want instead of /*, e.g. ./ for current directory or ./* for each item in current directory. – psur Aug 9 '12 at 6:22
  • 3
    @psur or you can use ./*/ to get only subfolders and not all items – relascope Jan 28 '16 at 23:48
  • 7
    Sorted version: du -sh /* | sort -h – Vu Anh Jul 28 '17 at 3:24
  • 2
    @c1phr If your sort doesn't have -h, you need to leave it off from du as well, otherwise the sorting will mix up kilo/mega/gigabytes. du -s /* | sort -nr. – Thomas Aug 20 '18 at 12:30
80

I often need to find the biggest directories, so to get a sorted list containing the 20 biggest dirs I do this:

du -m /some/path | sort -nr | head -n 20

In this case the sizes will be reported in megabytes.

  • 11
    Here's a way to get it more readable du -sh /some/path | sort -hr | head -n 20 – Xedecimal Jul 8 '13 at 15:49
  • 6
    @Xedecima the problem with using h is the sort doesn't know how to handle different sizes. For example 268K is sorted higher than 255M, and both are sorted higher than 2.7G – chrisan Dec 27 '13 at 18:33
  • 4
    The -h (human readable) argument on the 'sort' command should properly read these values. Just like du's -h flag exports them. Depending on what you're running I'm guessing. – Xedecimal Apr 14 '14 at 18:02
  • Works in Ubuntu 16.04. Nice tip. – SDsolar Mar 4 '18 at 17:15
  • sudo du -haxt 1G / | sort -hr | head -30 – deviant Oct 5 '18 at 10:03
22

I like to use Ncdu for that, you can use the cursor to navigate and drill down through the directory structure it works really well.

11

The existing answers are very helpful, maybe some beginner (like me) will find this helpful as well.

  1. Very basic loop, but for me this was a good start for some other size related operations:

    for each in $(ls) ; do du -hs "$each" ; done
    
  2. Very similar to the first answer and nearly the same result as 1.), but it took me some time to understand the difference of * to ./* if in a subdirectory:

    du -sh ./*
    
  • for each does not work as it appends console characters (eg \033[) to the list of folders – machineaddict Feb 6 at 11:57
  • @machineaddict not sure what you mean. I use this all the time, works for me just fine. – Martin Feb 6 at 12:57
  • try to run your command starting with for each. it will not work – machineaddict Feb 6 at 14:34
  • i run the command exactly as written here. starting with for each. works. – Martin Feb 7 at 14:29
7

The following du invocation should work on BSD systems:

du -d 1 /
  • 1
    My du (Ubuntu 10.4) doesn't have a -d option. What system are you on? – Thomas Jul 11 '10 at 17:30
  • On my openSUSE it doesn't have a -d option either :( – 2ndkauboy Jul 11 '10 at 17:33
  • OK, then it's a BSD option only (I'm on OS X). – Philipp Jul 11 '10 at 17:37
  • Right portable option combination on BSD/*NIX is du -sk /*. I hate the -k stuff soooo much. Linux' -h totally rocks. – Dummy00001 Jul 11 '10 at 19:46
  • in other systems, its --max-depth – Vishnu Kumar Sep 1 '15 at 12:49
4

This isn't easy. The du command either shows files and folders (default) or just the sizes of all items which you specify on the command line (option -s).

To get the largest items (files and folders), sorted, with human readable sizes on Linux:

du -h | sort -h

This will bury you in a ton of small files. You can get rid of them with --threshold (1 MB in my example):

du --threshold=1M -h | sort -h

The advantage of this command is that it includes hidden dot folders (folders which start with .).

If you really just want the folders, you need to use find but this can be very, very slow since du will have to scan many folders several times:

find . -type d -print0 | sort -z | xargs --null -I '{}' du -sh '{}' | sort -h
  • 1
    --threshold ^^^ this option is not availavle on linux – podarok Oct 6 '15 at 12:30
  • 1
    @podarok It's available on OpenSUSE 13.2 Linux. Try to find a more recent version of your distribution or compile a more recent version of the package yourself. – Aaron Digulla Oct 7 '15 at 8:51
  • It doen't work on Ubuntu LTS (14.04). It is the most recent one )) – podarok Oct 7 '15 at 13:16
  • @podarok Which version of GNU coreutils? Mine is 8.24. – Aaron Digulla Oct 8 '15 at 7:33
  • 1
    Caching might have been a bad term. I was thinking of something like done in this port superuser.com/a/597173/121352 where we scan the disks contents once into a mapping and then continue using data from that mapping rather than hitting the disk again. – Hennes Jan 12 '16 at 14:09
1

Be aware, that you can't compare directories with du on different systems/machines without getting sure, both share the same blocksize of the filesystem. This might count if you rsync some files from a linux machine to a nas and you want to compare the synced directory on your own. You might get different results with du because of different blocksizes....

0

You might also want to check out xdiskusage. Will give you the same information, but shown graphically, plus allows to drill down (very useful). There are other similar utilities for KDE and even Windows.

0

You could use ls in conjunction with awk:

ls -al * | awk 'BEGIN {tot=0;} {tot = tot + $5;} END {printf ("%.2fMb\n",tot/1024/1024);}'

The output of ls is piped to awk. awk starts processing the data. Standard delimiter is space. The sum variable tot is initialised to zero; the following statement is executed for each row/line outputted by ls. It merely increments tot with the size. $5 stands for fifth column (outputted by ls). At the end we divide by (1024*1024) to sum in megabytes.

If you would convert this into a script or function (.bashrc) you can also use it to get the size of certain subsets of directories, according to filetypes.

If you want system wide information, kdirstat may came in handy!

  • I agree one can expand this example and do tricks like getting the size of "certain subsets of directories, according to filetypes" etc.; it may seem a good starting point. Nevertheless this solution is flawed from the start. To every user who would like to use this method I recommend reading answers and comments to this question as well as the article linked there. I don't say you cannot do it at all. Know the limitations, that's all. – Kamil Maciorowski Dec 22 '16 at 18:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.