How do I get the size of a Linux or Mac OS X directory from the command-line?

76

36

What command do I use to find the size of all the files (recursively) in a Linux or Mac OS X directory?

Daryl Spitzer

Posted 2009-08-13T20:00:15.147

Reputation: 7 505

Question was closed 2015-03-11T16:15:31.513

2you can use -k option to use block size of 1K-byte – Zoker – 2016-12-29T10:37:12.840

I just want to recommend this free software called Disk Inventory X. download it here http://www.derlien.com/ it's simple to use for mac osx

– Nimitack – 2018-01-06T23:05:32.720

Instead of du, I use ncdu, you can find the reference here, https://remysharp.com/2018/08/23/cli-improved

– Alan Dong – 2018-09-19T23:28:45.127

Answers

83

The BSD version of 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.

Here's an example.

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. (There are no weird forks or xattrs in this set of files.) It's significantly smaller than the value reported by du.

Here's how it works: it gets a list of all the files, and passes them to ls -l; then awk is used to count up the bytes. The -type f flag is there so that only files (and not directories) get sent to ls. Without that flag, it'll also send directory names to ls, and each file will be listed twice : once as an individual file, and once as an item in the directory.

The GNU version of du can give values in actual bytes instead of blocks. It's unfortunate that the BSD version of du is not as flexible.

wch

Posted 2009-08-13T20:00:15.147

Reputation: 946

is there any way to print this out in a more human readable format? – anon58192932 – 2017-04-26T21:42:46.030

Great explanation. Interestingly when using du from GNU coreutils with the -b option I get a different result than with your find pipe. For a 36 GB directory I get a 82 KB difference. – Stefan Schmidt – 2012-07-25T22:15:50.503

75

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

Daryl Spitzer

Posted 2009-08-13T20:00:15.147

Reputation: 7 505

25du -sch if you want something more easily readable. – Joey – 2009-08-13T20:09:31.597

@Johannes, I guess you should post this comment as an answer, so it can be accepted? – Arjan – 2009-08-15T17:15:38.363

I find the -c option redundant (at least on Mac OS X) and du -sh supresses the subtotals. – Daryl Spitzer – 2009-08-17T18:35:58.367

Aha, I didn't know one could accept one's own answer. Now at least people know the question is solved. – Arjan – 2009-08-18T09:41:07.580

I'll happily accept a new answer that is an improvement on mine. – Daryl Spitzer – 2009-08-18T16:10:30.703

9

du - tells the disk use not the file size.

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

above terminal code (im on osx 10.6) offers for me the best result and is waaay faster than "find ... -exec"

a quick benchmark

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

real    0m0.086s
user    0m0.029s
sys 0m0.073s

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

real    0m18.515s
user    0m2.929s
sys 0m9.339s

Acid

Posted 2009-08-13T20:00:15.147

Reputation: 91

If you want to do the same on Ubuntu: find . -type f -print0 | xargs -0 stat -c%s | awk '{b+=$1} END {print b}' – Rodrigo Polo – 2015-05-04T12:51:48.647

is there a way to get this result in a human-readable format? – anon58192932 – 2017-04-26T21:46:08.420

6

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

Note: Option -h for sort is not available on OSX/BSD, so you've to install sort from coreutils (e.g. via brew) and apply the bin path to PATH, e.g.

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" # Add a "gnubin" for coreutils.

Otherwise use:

du -a . | sort -rn | head -20

kenorb

Posted 2009-08-13T20:00:15.147

Reputation: 16 795

First command not work on my macOS 10.12.1 #du -ah . | sort -rh | head -20 sort: invalid option -- h Try `sort --help' for more information. – nucleartux – 2016-11-24T15:11:23.130

3@nucleartux Check the note below the command. This works in GNU sort which you can install on macOS via: brew install coreutils, otherwise use the BSD sort command at the end without -h. – kenorb – 2016-11-24T15:44:20.967

2

I combined all your approuches and combined it with a human readable output the result is:

#!/bin/sh
find $1 -type f -print0 | xargs -0 stat -f'%z' | awk '{b+=$1} END {print b}' | awk '{ sum=$1 ; hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}'

Link to the gist: https://gist.github.com/mlegenhausen/9365461

malte

Posted 2009-08-13T20:00:15.147

Reputation: 21