DEV Community

loading...

Command Line to List Size of Directory (MacOS/Linux)

Nic
Programming for 16 years. Write stuff about programming and writing on coderscat.com. Programming languages, security, algorithms.
Originally published at coderscat.com on ・1 min read

2020_03_31_command-to-list-size-of-directory.org_20200331_194953.png

I found the command du is different on Mac compared with Linux. A little bit of confusion.

Here is a note how to use du on different OS.

du on Mac

List the size of current directory, the option -h is displaying in human friendly format, the option -s is not recursive to display all sub-directories:

du -hs .
Enter fullscreen mode Exit fullscreen mode

List all the size of sub-directories in current directory:

find . -maxdepth 1 -mindepth 1 -type d -exec du -hs {} \;
Enter fullscreen mode Exit fullscreen mode

If you want to sort according to the size, use pipeline and sort:

find . -maxdepth 1 -mindepth 1 -type d | xargs du -hs | sort -hr
Enter fullscreen mode Exit fullscreen mode

If you want to know which directory cost most disk space, another handy util command line is ncdu. You could install it via homebrew:

brew install ncdu

# Run in the specific directory
ncdu
Enter fullscreen mode Exit fullscreen mode

The result is like this:

2020_03_31_command-to-list-size-of-directory.org_20200331_194324.png

du on Linux

Linux du have the option –max-depth to limit the depth:

du -h --max-depth=1 | sort -hr
Enter fullscreen mode Exit fullscreen mode

The post Command Line to List Size of Directory (MacOS/Linux) appeared first on Coder's Cat.

Discussion (1)