Unix & Linux: How do I get the size of a directory on the command line? (16 Solutions!!)
Channel: Roel Van de Paar
Date: 06/08/2020

Unix & Linux: How do I get the size of a directory on the command line?


The Question: I tried to obtain the size of a directory (containing directories and sub
directories) by using the ls command with option l. It seems to work for files
(ls -l file name), but if I try to get the size of a directory (for instance,
ls -l /home), I get only 4096 bytes, although altogether it is much bigger.

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 1615 people ==
du -sh file_path
Explanation
 * du (disc usage) command estimates file_path space usage
 * The options -sh are (from man du):
 -s, --summarize
 display only a total for each argument

 -h, --human-readable
 print sizes in human readable format (e.g., 1K 234M 2G)
 To check more than one directory and see the total, use du -sch:
 -c, --total
 produce a grand total

== This solution helped 219 people ==
du is your friend. If you just want to know the total size of a directory then
jump into it and run:
du -hs
If you also would like to know which sub-folders take up how much disk space?!
You could extend this command to:
du -h --max-depth=1 | sort -hr
which will give you the size of all sub-folders (level 1). The output will be
sorted (largest folder on top).

== This solution helped 28 people ==
The du command shows the disk usage of the file.
The -h option shows results in human-readable form (e.g., 4k, 5M, 3G).
du -h (file name)

== This solution helped 357 people ==
Just use the du command:
du -sh -- *
will give you the cumulative disk usage of all non-hidden directories, files
etc in the current directory in human-readable format.
You can use the df command to know the free space in the filesystem containing
the directory:
df -h .

== This solution helped 35 people ==
Others have mentioned du, but I would also like to mention http://
dev.yorhel.nl/ncdu/scr -- which is an ncurses version of du and provides
interactivity: You can explore the directory hierarchy directly and see the
sizes of subdirectories.

== This solution helped 10 people ==
I http://explainshell.com/explain?cmd=sudo+ls+-1d+*%2F+%7C+sudo+xargs+-
I%7B%7D+du+%7B%7D+-sh+%26%26+csudo+du+-sh:
sudo ls -1d */ | sudo xargs -I{} du {} -sh && sudo du -sh

== This solution helped 17 people ==
personally I think this is best, if you don't want to use ncdu
# du -sh ./*

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user TRiG (https://unix.stackexchange.com/users/1548), user Teque5 (https://unix.stackexchange.com/users/153452), user Tblue (https://unix.stackexchange.com/users/95604), user Steven Penny (https://unix.stackexchange.com/users/17307), user Stephen Rauch (https://unix.stackexchange.com/users/208146), user Stephane Chazelas (https://unix.stackexchange.com/users/22565), user SDsolar (https://unix.stackexchange.com/users/197427), user sam (https://unix.stackexchange.com/users/103781), user Pacifist (https://unix.stackexchange.com/users/103470), user mpen (https://unix.stackexchange.com/users/45556), user Lucifer (https://unix.stackexchange.com/users/235153), user Leon Chang (https://unix.stackexchange.com/users/204134), user Kamil Kielczewski (https://unix.stackexchange.com/users/188291), user jackotonye (https://unix.stackexchange.com/users/103644), user feldversuch (https://unix.stackexchange.com/users/189054), user Eduard Florinescu (https://unix.stackexchange.com/users/22558), user datatype_void (https://unix.stackexchange.com/users/244552), user ConnerMan (https://unix.stackexchange.com/users/157321), user Chrispie (https://unix.stackexchange.com/users/48433), user arupgsh (https://unix.stackexchange.com/users/191443), user anton_rh (https://unix.stackexchange.com/users/152606), user Andy Foster (https://unix.stackexchange.com/users/171672), user Abdul Al Hazred (https://unix.stackexchange.com/users/102788), user 0x8BADF00D (https://unix.stackexchange.com/users/79090), and the Stack Exchange Network (http://unix.stackexchange.com/questions/185764). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.