The du command reports how much disk space your files and folders take up, which makes it the fastest way to see what fills a disk. Combine it with sort to put the largest folders first. Pair it with df to see how much free space you still have.
What it does
A disk that is almost full raises one question: which folders are the big ones? Desktop users can answer it with a disk usage app. In the terminal, the tool is du, short for "disk usage".
Picture a moving company that has to count the boxes in a house. The movers can give one number for the whole house, or a number for each room. du works the same way on your disk. It measures one folder as a whole, or each folder inside it.
The command is safe to try. It only reads and never deletes or changes a file. Once you find a large folder, the choice of what to do with it stays with you.
Back to topHow to use it
This shows the total size of one folder in readable units (KB, MB, GB):
du -sh ~/Downloads
-s stands for "summary" (the total only, no subfolders). -h stands for "human-readable".
For each folder inside your home folder, with the largest at the bottom:
du -h --max-depth=1 ~ | sort -h
--max-depth=1 limits the output to one level of folders, and sort -h orders sizes like 2.1G and 512M correctly.
For all files and folders in the current folder:
du -sh * | sort -h
Be careful: * skips hidden folders, whose names begin with a dot. Big hidden folders such as .cache are common. Run du -sh .[!.]* * | sort -h to count them too.
A scan of a whole disk needs sudo, because only root can read some folders. -x keeps du on one file system, so other disks and network shares stay out of the count:
sudo du -xh --max-depth=1 / | sort -h
The related command df shows the free space on each disk:
df -h
Back to topHow it works
Internally, du walks the folder tree. For each file, it asks the file system how many disk blocks the file occupies. That number is the real space on the disk. It is often a little larger than the file size, because the file system hands out space in blocks, usually of 4 KB. Add --apparent-size if you want the file sizes instead.
A file with several hard links is counted only once. Some backup tools use hard links to save space, so this detail matters there.
The numbers from du and df do not always match. These are the usual causes:
- Deleted files that are still open. A program can keep a deleted file open, for example a large log file. The space stays in use until the program closes the file.
dfincludes it, butducannot see it. Runsudo lsof +L1to find such files, then restart the program. - Files hidden under a mount point. If another disk is mounted on top of a folder, the files inside that folder become invisible. They still take up space.
- Reserved blocks. By default, ext4 sets aside 5% of the space for root.
dfleaves this space out of the available total. - Snapshots and compression. On Btrfs and ZFS, snapshots are invisible to
du, and compressed files take less space thandureports. Use the file system's own tools instead, such asbtrfs filesystem duorcompsize.
On a large tree, du can be slow, since it must read the details of every file. ncdu does the same counting, then lets you browse the result and delete folders interactively.
