The du command
The du command shows how much disk space files and folders take…
The du command shows how much disk space files and folders take; du -sh folder prints one readable total, such as 12K or 1.5G.
Taught in Intermediate, lesson 19; more in Ninja
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
du [OPTION]... [FILE]...
Show how much disk space files and folders use. Adds up the space each folder takes on the disk, including everything inside it, and prints one line per folder, the deepest first, ending with the folder you named (or the current one). Sizes are in kilobytes unless you add -h. Disks store files in blocks of 4K, so even a tiny file counts as 4K.
How to use du
The size of a folder: du -sh
-s gives one total and -h makes it readable.
du -sh projects32K projects
Every folder inside, with sizes
du lists the deepest folders first and the one you named last.
du -h projects8.0K projects/app/config 16K projects/app 4.0K projects/config 8.0K projects/notes 32K projects
Find what takes the most space
List everything here with its size, then sort by size.
du -s * | sort -n | tail -n 38 website 16 build 32 projects
Only one level down: du -d 1
du -h -d 1 projects16K projects/app 4.0K projects/config 8.0K projects/notes 32K projects
Options
| -h, --human-readable | sizes like 4.0K, 12K or 1.5M |
|---|---|
| -s, --summarize | one total for each folder you name, not every folder inside |
| -a, --all | list files too, not only folders |
| -c, --total | add a grand total at the end |
| -d N, --max-depth=N | list folders only down to N levels below the start |
More examples
du -sh *4.0K a.tmp 4.0K app.log 4.0K archive 4.0K b.tmp 4.0K backup.sh 4.0K beach.jpg 4.0K bin 16K build 4.0K cat.jpg 4.0K data.csv 4.0K diary.txt 4.0K draft.txt … and 21 more lines
du -ah build4.0K build/cache/x.o 8.0K build/cache 4.0K build/output.bin 16K build
Questions about du
What is the difference between du and df?
du adds up the space used by the files you name; df shows how full each whole disk is.
Why does du show 4.0K for a tiny file?
Disks store files in blocks, usually 4K each, so even a one-byte file uses a whole block. ls -l shows the bytes in the file; du shows the space it takes.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.