415

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?

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
2ndkauboy
  • 4,343
  • 4
  • 17
  • 8
  • 2
    The `--total` flag was helpful for me. E.g. `du -sh --total applications/*`. https://askubuntu.com/a/465436/48214 – Ryan Jul 07 '18 at 15:29

9 Answers9

595

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.
Thomas
  • 6,499
  • 2
  • 17
  • 16
  • 9
    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
  • 13
    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 09 '12 at 06:22
  • 5
    @psur or you can use `./*/` to get only subfolders and not all items – relascope Jan 28 '16 at 23:48
  • 11
    Sorted version: `du -sh /* | sort -h` – Vu Anh Jul 28 '17 at 03:24
  • Nice tip on the `sort -h` – SDsolar Mar 04 '18 at 17:14
  • If you're using alpine, `-h` isn't an available flag on `sort`. `sort -nr` should get you the same result, leading to `du -sh /* | sort -nr`. – c1phr Aug 20 '18 at 11:23
  • 4
    @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
  • 1
    This should throw the top ten of biggest directories: `du -hS /var/lib/docker | sort -rh | head -10` – joseluisq Jan 30 '19 at 07:48
  • I've ended with `du -hs /* | sort -hr`. It outputs sizes in readable format (5GB, 6.6M) and sorts values descending. – userlond Jul 08 '19 at 08:21
  • This does not show hidden folder. To get all folder: `du -sh .[^.]* * | sort -h` – Jotne Aug 16 '22 at 20:06
  • How would you adjust this answer to ignore files, and ONLY show directory sizes? – rlittles Jun 01 '23 at 03:36
  • 1
    @rlittles That would be a good topic for a new question, but: `find / -mindepth 1 -maxdepth 1 -type d -exec du -sh '{}' \; | sort -h` – Thomas Jun 01 '23 at 07:09
  • To include hidden files, use ```du -sh $(ls -A)``` after entering that directory. – Gautam Sreekumar Jun 21 '23 at 18:44
  • 1
    @GautamSreekumar That only works if none of the file names contain spaces. `(shopt -s dotglob; du -sh *)` will do the trick more safely. – Thomas Jun 22 '23 at 07:05
99

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.

Brad Koch
  • 151
  • 9
Janne Pikkarainen
  • 7,715
  • 1
  • 31
  • 32
  • 17
    Here's a way to get it more readable du -sh /some/path | sort -hr | head -n 20 – Xedecimal Jul 08 '13 at 15:49
  • 8
    @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
  • 5
    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
  • 1
    Works in Ubuntu 16.04. Nice tip. – SDsolar Mar 04 '18 at 17:15
  • 2
    sudo du -haxt 1G / | sort -hr | head -30 – deviant Oct 05 '18 at 10:03
32

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.

g2mk
  • 1,428
  • 12
  • 15
BuddhaLincoln
  • 421
  • 4
  • 2
19

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 ./*
    
Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
Martin
  • 295
  • 2
  • 7
10

The following du invocation should work on BSD systems:

du -d 1 /
Philipp
  • 271
  • 1
  • 7
7

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
Aaron Digulla
  • 6,925
  • 8
  • 45
  • 66
  • 2
    --threshold ^^^ this option is not availavle on linux – podarok Oct 06 '15 at 12:30
  • 2
    @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 07 '15 at 08:51
  • 1
    It doen't work on Ubuntu LTS (14.04). It is the most recent one )) – podarok Oct 07 '15 at 13:16
  • 1
    @podarok Which version of GNU coreutils? Mine is 8.24. – Aaron Digulla Oct 08 '15 at 07:33
  • Sorry. It was not on Ubuntu 14.04. It was Centos 6.6. Lost in consoles.... – podarok Oct 08 '15 at 09:49
  • Yes, that can be very slow. But you can cache the output from a single find and work from that. No need to access the filesystem several times. – Hennes Dec 24 '15 at 10:23
  • @Hennes: How do you cache file sizes in a shell script? – Aaron Digulla Jan 12 '16 at 14:03
  • 2
    Caching might have been a bad term. I was thinking of something like done in this port http://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
2

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.

sleske
  • 22,652
  • 10
  • 69
  • 93
2

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....

Jimmy Koerting
  • 211
  • 2
  • 6
1

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!

  • 1
    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](http://unix.stackexchange.com/q/128985/108618) as well as the [article linked there](http://mywiki.wooledge.org/ParsingLs). I don't say you cannot do it at all. Know the limitations, that's all. – Kamil Maciorowski Dec 22 '16 at 18:42