Linux commands: du
By Flavio
Published · Updated
Tagged: CLI
Learn the Linux du command: get a folder's total size with du -sh, list the biggest folders with du -h -d 1 | sort -hr, exclude folders, and why du and ls show different sizes.
The du command will calculate the size of a directory as a whole:
du

The 32 number here is not bytes. du counts disk blocks: 512-byte blocks on macOS, 1 KB blocks on most Linux systems. That’s why I always add -h, as we’ll see in a moment.
Running du * will calculate the size of each file individually:

You can set du to display values in kilobytes with du -k and megabytes with du -m. macOS also has du -g for gigabytes, but GNU du on Linux doesn’t: you get invalid option -- 'g'.
The -h option will show a human-readable notation for sizes, adapting to the size:

Adding the -a option will print the size of each file in the directories, too:

Get the total size of a folder
By default du prints a line for every subfolder. Most of the time you just want one number. Use -s (summarize):
du -sh node_modules
2.0M node_modules
Pass several paths and you get one line each. Add -c to get a grand total at the end:
du -ch file1 file2 folder
Find the biggest folders
A handy thing is to sort the directories by size:
du -h <directory> | sort -nr
and then piping to head to only get the first 10 results:

This works here because every size is in K. sort -nr only compares the numbers, so 540K would sort above 11M. When the sizes mix units, use sort -hr instead: it understands the K, M and G suffixes, and both Linux and macOS support it.
That command lists every folder at every depth, which gets long. To only see the folders one level down, use -d 1:
du -h -d 1 . | sort -hr | head -n 10
2.3M .
2.0M ./node_modules
300K ./.cache
4.0K ./src
-d works on both Linux and macOS. On Linux you can also write --max-depth=1.
You’ll also see this version, which lists every file and folder in the current folder:
du -sh * | sort -hr
Careful: * skips hidden files and folders, like .git or .cache, which are often the big ones. The -d 1 version above includes them.
Hide the “Permission denied” errors
When you run du on a folder with files you can’t read, you get a wall of errors:
du: /private/var/root/Library/Application Support/AddressBook: Permission denied
The total is still printed, but it doesn’t count what du couldn’t read. Hide the errors by sending them to /dev/null:
du -sh /some/folder 2>/dev/null
or run it with sudo to get the real number.
Exclude a folder
On Linux:
du -sh --exclude=node_modules .
On macOS, use -I instead:
du -sh -I node_modules .
Stay on one disk
du / also walks into other mounted disks, network shares and USB drives. -x makes it stay on the file system you started from:
sudo du -xh -d 1 / | sort -hr | head
Why du and ls show different sizes
ls -l shows a file’s size in bytes. du shows how much disk space it uses, and disks store files in blocks. A file with 8 bytes in it still takes a whole block:
ls -l b.txt # 8 bytes
du -h b.txt # 4.0K
So a folder with lots of tiny files looks much bigger in du. If you want the sum of the real file sizes instead, use --apparent-size on Linux or -A on macOS.
du vs df
du adds up the size of the files you point it at. df asks the file system how much space is used and free on the whole disk. Use df -h to see if a disk is full, then du to find what’s filling it.
The
ducommand works on Linux, macOS, WSL, and anywhere you have a UNIX environment
Want me to talk about your product? You can sponsor this site.
Related posts about cli: