Linux Command Line · Lesson 30 of 43
Disk and memory: df, du and free
Check free disk space with df, find what uses it with du and sort, and read memory use with free, including what buffers and cache really mean.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
When a server gets slow, a download fails or a log stops being written, the cause is often a full disk or too little memory. You will learn to check both in seconds and to find what is using the space. These are among the first commands to run in any troubleshooting session.
Free space and memory are different on every machine, so most of the outputs below are examples, labelled as such. The commands themselves are exactly what you would type.
df: free space on filesystems
df (disk free) lists each mounted filesystem with its size, used space and free space. Add -h for human-readable units:
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 98G 41G 53G 44% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/sda1 511M 6.1M 505M 2% /boot/efiRead it as: the root filesystem / has 98 GB, of which 44% is used. The last column shows where each filesystem is mounted. To ask about the place you are in, give a path: df -h .. Watch the Use% column: above about 90% is a warning sign, and at 100% programs fail in odd ways, such as being unable to write logs or create temporary files.
df -i shows inodes, the file entries, instead of bytes. A folder with millions of tiny files can use up all the inodes while plenty of space remains:
df -ih /du: what is using the space
du (disk usage) adds up how much space files and folders use. -s gives one total per argument and -h makes it readable:
cd ~/project
du -b data/fruit.txt data/scores.csv44 data/fruit.txt
94 data/scores.csvWe used -b here, which prints exact bytes (a GNU option; macOS du has no -b), so the result is the same on every Linux system. Without it, du reports disk blocks, which round each file up to the filesystem's block size, usually 4 KB, so a 44-byte file shows as 4.0K:
du -sh project
60K projectTo find the biggest items in a folder, summarise each entry and sort by size. sort -h understands units such as K, M and G:
du -sh * | sort -h
4.0K README.md
12K docs
16K src
28K dataAdd -r to put the biggest first, and | head -n 5 to see only the top five. To dig deeper, repeat the command inside the biggest folder. du -h --max-depth=1 shows each subfolder one level down, and ncdu is an interactive explorer if it is installed. Use sudo du ... 2>/dev/null for system folders, so that permission errors do not clutter the list.
A word of warning: du counts what it can read, and deleted files that a running program still holds open keep using space until the program closes them. If df and du disagree, that is a common reason, and restarting the service fixes it.
free: memory
free reports memory in kibibytes (1024-byte units) by default, and -h makes it readable:
free -h
total used free shared buff/cache available
Mem: 7.7Gi 2.1Gi 1.2Gi 112Mi 4.4Gi 5.3Gi
Swap: 2.0Gi 0B 2.0GiThe columns can be confusing:
- total: the installed memory.
- used: memory in use (current versions of
freecalculate it as total minus available; older versions subtracted free, buffers and cache instead). - free: memory that is completely unused. This number is often small, and that is normal.
- buff/cache: memory Linux uses to keep recently read files, which makes the system faster. Most of it can be released when programs need the memory.
- available: an estimate of how much memory programs could get, including the cache that can be freed. This is the number to watch.
New users often see a small free value and think the machine is out of memory. Look at available instead. Swap is disk space used as overflow memory. A little swap use is fine, but heavy swapping makes a system crawl, and it means you really are short of RAM.
Other ways to look: top and ps aux --sort=-%mem | head show which processes use the most memory, and /proc/meminfo has every detail. uptime shows how long the machine has run and its load averages, and nproc the number of CPU cores.
A quick health check
When a machine misbehaves, run these four commands first and look at the output:
df -h
df -ih
free -h
uptimeIs the disk nearly full? Are inodes exhausted? Is available memory close to zero while swap is heavily used? Is the load average well above the number of cores? These answer most "why is it slow?" questions before you look any deeper.
Cleaning up safely
Before deleting anything, find out what it is. Common safe candidates are package caches (sudo apt clean), old logs in /var/log (rotate rather than delete), and old downloads in your own home folder. Never delete files in a system folder just because they are big, and never empty a log file by deleting it while a service writes to it; truncate it with : > file or truncate -s 0 file instead, so the program keeps a valid file.
Common mistakes
Reading free instead of available. Low free memory is normal on Linux because of the cache.
Comparing du with df. They measure different things: files you can see versus whole filesystems.
Deleting a big log that a program still has open. The space is not freed until the program closes it.
Forgetting inodes. A disk can be full of files without being full of bytes.
Exercise
Run df -h and name the filesystem that holds your home folder. In your home folder, run du -sh * | sort -h and name your three biggest items. Run free -h, find the available number and explain why free is not the right figure to worry about.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.