Linux Basics Lesson 28 of 38
Where has the space gone
9 minutes
Learning goals
- Read df -h, df -T and df -i, and say what each one shows.
- Follow the space level by level with du -h -d 1 and du -ah.
- Tell apart a block device and a mount point with lsblk, mount and findmnt.
df: confirm
df looks at whole file systems, not at folders:
df -h
Purpose: Shows how full each file system is.
Syntax: df [-h | -T | -i] [PATH]
Options:
Example:
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 40G 39G 1.4G 97% /
tmpfs 392M 1.1M 391M 1% /run
tmpfs 2.0G 4.0K 2.0G 1% /dev/shm
tmpfs 512M 4.0K 512M 1% /tmp/dev/sda1 is mounted on / and is at 97 %. The tmpfs lines are in RAM and do not count here, including /tmp, which has room of its own. So check the file system that the path is on. df -h /var/log tells you which one that is.
df -T adds the type, ext4 here, xfs on many RHEL and Rocky Linux servers. df -i counts inodes instead of space. Every file needs one, and a folder with millions of tiny files can make a disk "full" although df -h still shows free room. If writing fails with No space left on device and the space looks fine, look there next.
du: narrow it down
du adds up what lies in folders. One level at a time is the quickest route:
du -h -d 1 /var 2>/dev/null
Purpose: Adds up what is in folders, one level at a time.
Syntax: du [-h] [-d N] [-a] [PATH]
Options:
Example:
du -h -d 1 /var 2>/dev/null
8.0K /var/backups
4.0K /var/cache
4.0K /var/lib
4.0K /var/local
31G /var/log
4.0K /var/mail
4.0K /var/opt
12K /var/spool
4.0K /var/tmp
31G /var2>/dev/null hides the messages about folders that lerner may not read, otherwise they would scroll the numbers away. Follow the biggest number. Here that is /var/log, 31G. Then one level further down, and with -a also the individual files:
du -ah /var/log/app
1.9G /var/log/app/app.log
29G /var/log/app/app.log.1
31G /var/log/app
On a real server, you shorten the walk with a pipe, and you will type this line often: du -sh /var/* 2>/dev/null | sort -rh | head. It sorts the biggest to the top, and that is why sort -h understands 1.9G and 29G. It does not run on this page: sort and head answer with command not found. find /var -size +1G lists big files directly on a real server, and it is not loaded here either.
lsblk, mount, findmnt: what lies where
A block device is the hardware, or a part of it. A mount point is the folder where its file system appears. lsblk shows the first and links it to the second:
lsblk
Purpose: Lists the disks and partitions, and where they are mounted.
Syntax: lsblk | mount | findmnt [PATH]
Options:
Example:
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 40G 0 disk
└─sda1 8:1 0 40G 0 part /sda is the disk, sda1 the partition on it, and it is mounted on /. mount without arguments lists everything that is mounted, with the options, and on a real server that is a long list. findmnt / shows one entry:
findmnt /
TARGET SOURCE FSTYPE OPTIONS
/ /dev/sda1 ext4 rw,relatime
Changing anything here needs root: mounting, unmounting and partitioning all need sudo on a real server. Reading does not.
Practice
Terminal task 1/3
The chat says the disk is full. Confirm it first. How full is the file system that / is on?
Task 1/3: The chat says the disk is full. Confirm it first. How full is the file system that / is on?
The chat says the disk is full. Confirm it first. How full is the file system that / is on?
Task complete
Solution
df -h / Why: 97 % of 40 GiB, and only 1.4 GiB are left. Now you know that the alarm is real, and which file system it is about.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
df shows how full a file system is. Which folders take up the space is shown by:
df shows how full a file system is. Which folders take up the space is shown by: …
Check works once you have typed an answer.
Terminal task 2/3
Find the file that takes up the space. Start at /var and follow the biggest number, level by level, down to the files.
Task 2/3: Find the file that takes up the space. Start at /var and follow the biggest number, level by level, down to the files.
Find the file that takes up the space. Start at /var and follow the biggest number, level by level, down to the files.
Task complete
Solution
du -h -d 1 /var 2>/dev/null
du -ah /var/log/app Why: 29G in an old, rotated log. On a real server, du -sh /var/* 2>/dev/null | sort -rh | head takes you there faster, but it needs sort and head, and those are not on this page.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Which partition is / on? Show the block devices or the mount point, whichever you prefer.
Task 3/3: Which partition is / on? Show the block devices or the mount point, whichever you prefer.
Which partition is / on? Show the block devices or the mount point, whichever you prefer.
Task complete
Solution
lsblk Why: sda1 is part of sda and appears as /. Reading this needs no sudo. Changing anything does.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Predict the output
Command
du -sh /var/log/appBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
Check works once you have chosen an answer.
Put in order
A disk alarm. Put the steps in order.
Steps
- Confirm with df that it worked.
- Remove the cause, for example with log rotation.
- Check with ls -lh what you are about to touch.
- Make space, and spare whatever is still in use.
- Narrow it down with du, level by level.
- Confirm with df how full it really is.
Next up: packing things up and keeping copies.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 6 exercises, 3 quiz questions
What you can do now: df -h, du -h -d 1 /var 2>/dev/null, lsblk
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: done
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
df -hhow full each file system is -
df -iinodes instead of space -
du -h -d 1 /var 2>/dev/nullone total per folder -
du -ah <folder>single files as well -
lsblkdisks, partitions and where they are mounted
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 17 KB)
- View as image (PNG, 47 KB)