How to get the size of a Linux or Mac OS X directory from the command-line.

How to get the size of a Linux or Mac OS X directory from the command-line.

du used in OS X reports size with 512-byte blocks — the sizes are essentially rounded up to the next 512-byte value. This tells you the space on disk, which is larger than the amount of data. If you have a lot of small files, the difference can be large.

du – tells the disk use not the file size.

This is the value with regular du. It’s in 512-byte blocks:

$ du -s

248 .

The -h flag results in a more readable number, in kilobytes. As expected, it’s half the number of 512-byte blocks:

$ du -hs

124K .

Finally, you can use find and awk to give you the sum of actual bytes in the files. This is kind of slow, but it works:

$ find . -type f -exec ls -l {} \; | awk '{sum += $5} END {print sum}'

60527

This value matches exactly the number reported by Finder’s Get Info window.It’s significantly smaller than the value reported by du.

Show the size of a single file

du -h path_to_a_file

Show the size of the contents of a directory, each sub-directory, and each individual file:

du -h path_to_a_directory

Show the size of the contents of a directory:

du -sh path_to_a_directory

du -sch if you want something more easily readable

find . -type f -print0 | xargs -0 stat -f%z | awk '{b+=$1} END {print b}'

You can use du -ah . which displays sizes of all files and directories recursively.

This can be combined with sort, so you’ll see the top-20 biggest directories in the current folder:

du -ah . | sort -rh | head -20

tips & tricks

Join 7,714 other subscribers

interview questions


Algorithm Android Android Studio API APP Programming Apps AS400 blogging Browser CheatSheets Config CSS DATABASE dsa error Features GUI HTML HTML5 IDE installation Interview Questions IOS iPhone javascript Mac objective-c OneDrive OS Programming quicktips SDK SEO Settings SMO SQL swift swiftUI Teams Tips & Tricks Tools UI Web Wordpress Xcode


Archives

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading