Skip to content
Laviyx Learn

Try

Linux and Bash course menu

Linux Disk Space: df, du and Finding Large Files

Check free disk space with df, find what is using it with du, track down large files, list disks with lsblk, understand mounting, and free space safely.

Intermediate4 min readUpdated
On this page

"No space left on device" is one of the most common server problems. A full disk can stop databases, logs and deployments. Here is how to find what is using the space.

How much is free?#

df (disk free) shows each file system, and -h makes the sizes readable:

Terminal
df -h / | cut -c1-55
Output
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        72G   59G   13G  83% /

Watch the Use% column. A server above about 90% needs attention.

What is using it?#

du (disk usage) measures folders:

Terminal
du -sh data
du -sh data/*
Output
11M	data
2.9M	data/images
7.7M	data/logs
496K	data/tmp

-s gives one total for each argument. Add sort to see the biggest first:

Terminal
du -h --max-depth=1 data | sort -hr
Output
11M	data
7.7M	data/logs
2.9M	data/images
496K	data/tmp

sort -h understands sizes such as 7.7M and 496K. A common way to investigate is to start at / and go into the biggest folder each time:

Terminal
sudo du -h --max-depth=1 / 2> /dev/null | sort -hr | head

Find large files#

Terminal
find data -type f -size +1M -exec ls -lh {} + | awk '{print $5, $NF}'
Output
2.9M data/images/photo.raw
7.7M data/logs/app.log

Common space users#

LocationWhat to do
/var/logLimit logs: sudo journalctl --vacuum-size=200M
/var/cache/aptsudo apt clean
Dockerdocker system prune removes unused data
/tmpRemove old temporary files
node_modules, build foldersDelete them; they can be rebuilt

Deleted but still using space#

If df says the disk is full, but du shows much less, a process may still hold a deleted file open. This shows it, and restarting the process frees the space:

Terminal
sudo lsof +L1

Inodes#

Each file uses an inode, and a disk has a limited number of them. Millions of tiny files can fill the inodes, while df -h still shows free space. Check with:

Terminal
df -i / | awk 'NR == 2 {print "Inodes used:", $5}'
Output
Inodes used: 12%

Disks and partitions#

lsblk shows disks and where they are attached:

Terminal
lsblk -d -o NAME,SIZE,TYPE | head -n 3
Output
NAME SIZE TYPE
sda   75G disk

In Linux, a disk is mounted on a folder. That folder then shows the disk's contents:

Terminal
sudo mkdir /mnt/data
sudo mount /dev/sdb1 /mnt/data
sudo umount /mnt/data

To mount a disk at every boot, add it to /etc/fstab. A mistake in that file can stop the system from booting, so check it with sudo mount -a before restarting.

Common mistakes#

Practice#

Practice exercise

Print the size of the largest folder directly inside data, in human-readable form.

Show solution
Terminal
du -sh data/* | sort -hr | head -n 1
Output
7.7M	data/logs

Key takeaway#

Next: Archives with tar and gzip.

Finished this lesson?