Display each sub-directory size in a list format using one line command in Bash?

206,387

Solution 1

You probably want to see the directories ordered by size:

$ du -hs * | sort -hr

856M    lib
746M    share
612M    lib64
312M    src
267M    java
239M    bin
179M    sbin
173M    local
93M     i686-w64-mingw32
72M     libexec
26M     include
20M     puppet
772K    X11R6
20K     man
4.0K    games
4.0K    etc
0       tmp

Solution 2

Try this

du -h --max-depth=1

Output

oliver@home:/usr$ sudo du -h --max-depth=1
24M     ./include
20M     ./sbin
228M    ./local
4.0K    ./src
520M    ./lib
8.0K    ./games
1.3G    ./share
255M    ./bin
2.4G    .

Alternative

If --max-depth=1 is a bit too long for your taste, you can also try using:

du -h -s *

This uses -s (--summarize) and will only print the size of the folder itself by default. By passing all elements in the current working directory (*), it produces similar output as --max-depth=1 would:

Output

oliver@cloud:/usr$ sudo du -h -s *
255M    bin
8.0K    games
24M     include
520M    lib
0       lib64
228M    local
20M     sbin
1.3G    share
4.0K    src

The difference is subtle. The former approach will display the total size of the current working directory and the total size of all folders that are contained in it... but only up to a depth of 1.

The latter approach will calculate the total size of all passed items individually. Thus, it includes the symlink lib64 in the output, but excludes the hidden items (whose name start with a dot). It also lacks the total size for the current working directory, as that was not passed as an argument.

Solution 3

Worth to mention the NCurses Disk Usage shell command.

Ncdu is a disk usage analyzer with an ncurses interface. It is designed to find space hogs on a remote server where you don't have an entire graphical setup available, but it is a useful tool even on regular desktop systems. Ncdu aims to be fast, simple and easy to use, and should be able to run in any minimal POSIX-like environment with ncurses installed.

Solution 4

Print the sizes of all files folders and hidden files on disk:

el@dev /home/el $ du -sh `ls -a`
258M    .
265M    ..
4.0K    .classpath
258M    .git
4.0K    .gitignore
9.0K    nbactions.xml
12K     README
20K     .README.swp
4.0K    run.sh
23K    XmlPostPropagate.php
Share:
206,387

Related videos on Youtube

Patoshi パトシ
Author by

Patoshi パトシ

MIT graduate developer living and working from Tokyo! (◠‿◠) Open to freelance opportunities in Crypto or Drupal. Supporting: https://girlswhocode.com https://www.liftcommunities.org https://www.pursuit.org https://www.womenwhocode.com

Updated on September 18, 2022

Comments

  • Patoshi パトシ
    Patoshi パトシ over 1 year

    I want to get a list of the directories and their sizes in a list format like how you get when you do a ls -l.

    The thing is that is there a one line command that can do this? I see others have long commands just to output this. That's just too long.

    What command can do this or combination of commands that can be easily typed? du -h gives it, but it displays all of the sub-folders which is not what I want. just the current directories folders.

  • Dylan
    Dylan over 9 years
    Perfect answer. On Mac OSX the --depth parameter is not recognized. Use du -h -d 1 for the same result.
  • Kamil Maciorowski
    Kamil Maciorowski over 6 years
  • Chris Stryczynski
    Chris Stryczynski over 5 years
    This does not work if there large number of entries in the directory. bash: /run/current-system/sw/bin/du: Argument list too long