7

I want to check which directories take most disk space, quickly.

I tried du -sh subdir but it took more than 20 seconds on bigger directories.

I'm not sure how to display the size of all the subdirectories in the home directory at once with this method, but I'm afraid that it might take minutes...

Is there a fast way to do this?

I don't need to display the size of files, just directories.

  • To find out interactively what takes the most space, you could use ncdu – jfs Sep 3 '16 at 8:38
8

Sample directory

$ ls -aF
./  ../  .asd/  folder1/  folder2/  list  t1  t2  xyz/

To find sizes only for folders, excluding hidden folders:

$ find -type d -name '[!.]*' -exec du -sh {} + 
4.0K    ./folder1
4.0K    ./folder2
8.0K    ./xyz

If you need a total at the end as well:

$ find -type d -name '[!.]*' -exec du -ch {} + 
4.0K    ./folder1
4.0K    ./folder2
8.0K    ./xyz
16K total

To sort the results:

$ find -type d -name '[!.]*' -exec du -sh {} + | sort -h
4.0K    ./folder1
4.0K    ./folder2
8.0K    ./xyz

To reverse the sorting order:

$ find -type d -name '[!.]*' -exec du -sh {} + | sort -hr
8.0K    ./xyz
4.0K    ./folder2
4.0K    ./folder1

If you need with hidden directories as well, remove -name '[!.]*' from find command. I don't know any other command to find size of folders that is faster than du. Use df for file system disk space usage

Use find -maxdepth 1 -type d -name '[!.]*' -exec du -sh {} + to avoid sub-folders showing up

  • 1
    this is a good answer and can be easily adapted to show only folders from a certain user by doing -user username – rafaelvalle Jan 25 '18 at 4:43
  • Why would be $ find -type d -name '[!.]*' -exec du -sh {} + fast than just du -sh subdir? - isn't it the same command in the end? – Framester Jun 7 '18 at 13:17
  • 1
    @Framester sorry if that's your take away from the answer.. I've only illustrated use of find+du as OP asked display the size of all the subdirectories ... I mention in the answer that I do not know if there's a command that's faster than du.. use of find is to restrict what kind of files/directories you want to give to du – Sundeep Jun 7 '18 at 13:26
  • 1
    +1 for sort -h -- that's a great flag to know – spinup Nov 8 '18 at 18:47
5

Use package ncdu. you can install it by sudo apt-get install ncdu.
on server you can use it with options ncdu -q -x (Quiet Mode and Omit mounted directories).

Ncdu vs du/df

The interface of ncdu is built using ncurses and is interactive. Ncdu is different from df or du. Ncdu just does the one task of reporting the space used by a directory and drill down. On the other hand the df command reports space used by different storage devices.

So ncdu is a powerful tool to monitor, check and analyse disk space usage on your linux system

  • This looks like it may be useful, but is it faster than du? – spinup Nov 8 '18 at 19:00
4

All folders occupy the same amount of space, namely 4096 bytes. You don't want to know the size of the folders, but the size of what's in them. And this demands counting, which in turn demands time.

du counts directory sizes by default. So to get the sizes for it either:

cd && du

Or

du ~

See man du for more options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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