Skip to main content

The du Command: Find What Fills Your Disk

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 top

How 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 top

How 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. df includes it, but du cannot see it. Run sudo lsof +L1 to 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. df leaves 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 than du reports. Use the file system's own tools instead, such as btrfs filesystem du or compsize.

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.

Back to top

Sources

Back to top
Peter Martin
Linux admin, PHP dev and Joomla specialist

Open-source enthusiast since 2005 and co-founder of Linux Nijmegen (Netherlands), passionate about solving technical challenges and sharing knowledge.

Frequently asked questions

Yes. du only reads. It does not delete or change anything. When you have found a large folder, you decide yourself what to do with it.

Run du -h --max-depth=1 ~ | sort -h. It shows the size of each folder inside your home folder, with the largest last. --max-depth=1 shows one level of folders, and sort -h sorts sizes such as 2.1G and 512M correctly.

The * pattern skips hidden folders, whose names start with a dot. Large hidden folders, such as .cache, are common. To include them, use du -sh .[!.]* * | sort -h.

du shows how much space files and folders use. df shows how much space is free on each disk. Use df -h to see the free space in a readable form, such as GB.

Common reasons are deleted files that a program still keeps open, files hidden under a mount point, the 5% of space that ext4 reserves for root, and snapshots or compression on Btrfs and ZFS. To find deleted files that are still open, use sudo lsof +L1.