Linux Basics Lesson 8 of 38
Disk usage 94% at 6:50
8 minutes
Learning goals
- Go from coarse to fine with df and du, from the disk down to the single file.
- Make space without disturbing a running service, by emptying a file instead of deleting it.
- Confirm with df that the space is really free again.
Go from coarse to fine: df says that it is tight, du says where. The app runs as your user on this machine, so you do
not need sudo here. On most real servers you would, and that comes in module 5.
Why empty, not delete
On a real Linux system, a deleted file that a process still holds open does not give back its space. The name is gone, but the
data stays on the disk until the process closes the file, usually at its next restart. So you run rm, df still says 94%,
and you have lost the log as well. Emptying the file keeps the name and the open file, and the space is free at once.
This simulator does not model processes holding files open, so rm here would make df look fine. Do not let that fool you on
a real server. That is exactly why this scenario treats rm as the wrong answer.
There are two ways to empty a file. truncate -s 0 FILE says what it does. > FILE has the same effect and is shorter, but
one missing or extra character is expensive: > and >> look almost alike, and with the wrong file name you overwrite something
you needed.
Steps
Step 1/4: Confirm that it really is tight. How full is the disk that / is on? Step 2/4: Narrow it down. Find the biggest folder under /var and, inside it, the file that takes up the space. Step 3/4: Make space without disturbing the running app. Empty /var/log/app/debug.log, but keep the file. app.log stays untouched. Step 4/4: Confirm the success. How full is the disk now? Scenario solved
06:50. The monitoring wakes you: on web-01 only 6 percent of the disk are left. In a few hours the app will not be able to write any more. You want to know within a minute what is eating the space, and then get rid of it without taking the app down.
-
Step 1 done solved before
Confirm that it really is tight. How full is the disk that / is on?
Hint 1: df shows how full file systems are. -h makes the numbers readable.Hint 2: Type df -h /Solution
df -h /Why: Never trust an alarm blindly. A look at df takes a second and tells you the size of the problem, and which file system it is on.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 2 done solved before
Narrow it down. Find the biggest folder under /var and, inside it, the file that takes up the space.
Hint 1: du -sh with a pattern gives you one total per folder. Follow the biggest number, level by level. -a also lists single files.Hint 2: Type du -sh /var/* 2>/dev/null, then du -sh /var/log/* 2>/dev/null, then du -ah /var/log/appSolution
du -sh /var/* 2>/dev/null du -sh /var/log/* 2>/dev/null du -ah /var/log/appWhy: You saw it: 27 GiB of debug log that somebody left switched on while hunting a bug. Going level by level is quick and works on any machine. On a real server find /var -size +1G would have taken you straight there.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 3 done solved before
Make space without disturbing the running app. Empty /var/log/app/debug.log, but keep the file. app.log stays untouched.
Hint 1: You want the file to stay and its content to go. There is a command that sets the size of a file.Hint 2: Type truncate -s 0 /var/log/app/debug.logSolution
truncate -s 0 /var/log/app/debug.logWhy: rm is wrong here. On real Linux a running process keeps the data of a deleted file alive until it closes it, so the disk stays full. truncate frees the space at once, and the app goes on writing to the same file. Writing > debug.log has the same effect, but one wrong character there is expensive. Afterwards switch off the debug logging, or the file will grow back.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 4 done solved before
Confirm the success. How full is the disk now?
Hint 1: The same command as at the start.Hint 2: Type df -h /Solution
df -h /Why: A fix is only finished when you have seen the result. On a real server this is also the moment that reveals a process still holding deleted data, because the number would not move.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Scenario solved
Why each step mattered
- Step 1: Never trust an alarm blindly. A look at df takes a second and tells you the size of the problem, and which file system it is on.
- Step 2: You saw it: 27 GiB of debug log that somebody left switched on while hunting a bug. Going level by level is quick and works on any machine. On a real server find /var -size +1G would have taken you straight there.
- Step 3: rm is wrong here. On real Linux a running process keeps the data of a deleted file alive until it closes it, so the disk stays full. truncate frees the space at once, and the app goes on writing to the same file. Writing > debug.log has the same effect, but one wrong character there is expensive. Afterwards switch off the debug logging, or the file will grow back.
- Step 4: A fix is only finished when you have seen the result. On a real server this is also the moment that reveals a process still holding deleted data, because the number would not move.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Loading terminal …
Esc, then Tab leaves the terminal
Step 1 of 4
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.
Cheat sheet
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, 19 KB)
- View as image (PNG, 63 KB)