terminal.cab

Linux Basics Lesson 28 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Where has the space gone

9 minutes

Learning goals

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:

  • -h readable units
  • -T also the type of the file system
  • -i inodes instead of space

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

in the glossary: df

/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:

  • -d 1 only one level below the folder
  • -a single files as well
  • 2>/dev/null hides the messages about folders you may not read

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	/var

in the glossary: du

2>/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:

  • mount without arguments, lists what is mounted, with the options
  • findmnt / shows one mount point

Example:

lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda      8:0    0  40G  0 disk 
└─sda1   8:1    0  40G  0 part /

in the glossary: lsblk

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?

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

du -sh /var/log/app

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
What does du -sh /var/log/app print?

Check works once you have chosen an answer.

Put in order

A disk alarm. Put the steps in order.

Steps

  1. Confirm with df that it worked.
  2. Remove the cause, for example with log rotation.
  3. Check with ls -lh what you are about to touch.
  4. Make space, and spare whatever is still in use.
  5. Narrow it down with du, level by level.
  6. Confirm with df how full it really is.

Next up: packing things up and keeping copies.

Quiz

  1. Question 1 of 3Writing fails with No space left on device, but df -h shows 40 % in use. What do you check next?

    Check works once you have chosen an answer.

  2. Question 2 of 3/ is at 97 %, and /tmp is a tmpfs at 1 %. What does that tell you about /tmp?

    Check works once you have chosen an answer.

  3. Question 3 of 3df shows far more in use than du finds. What is a typical reason?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 29, Packing and mirroring (10 min)

What you can do now: df -h, du -h -d 1 /var 2>/dev/null, lsblk

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: done
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 29, Packing and mirroring (10 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.