The Computer Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Romantic Lands Beckon

--

Chapters
00:00 How Do I Get The Size Of A Linux Or Mac Os X Directory From The Command-Line?
00:17 Answer 1 Score 81
00:43 Accepted Answer Score 101
02:07 Answer 3 Score 11
02:30 Answer 4 Score 2
02:48 Thank you

--

Full question
https://superuser.com/questions/22460/ho...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #macos #mac #commandline

#avk47



ACCEPTED ANSWER

Score 101


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.




ANSWER 2

Score 81


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




ANSWER 3

Score 11


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



ANSWER 4

Score 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