Introduction
In this lab, you will learn essential Linux commands to check disk space and manage storage. You will begin by using the df -h command to understand overall disk usage on your system, viewing total, used, and available space in a human-readable format.
Next, you will explore how to measure the size of specific directories using the du -sh command, which is useful for identifying large directories consuming significant space. Finally, you will learn to list the partitions on your system by examining the contents of /proc/partitions, providing insight into your disk layout. These skills are fundamental for effective Linux system administration and troubleshooting disk-related issues.
Check disk usage with df -h
In this step, you will learn how to check the disk space usage on your Linux system using the df command. Understanding disk usage is crucial for managing your system and preventing issues caused by a full disk.
The df command stands for "disk filesystem". It displays information about the total space, used space, and available space on mounted filesystems.
Let's start by running the basic df command in your terminal:
df
You will see output similar to this:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 XXXXXXXX XXXXXXXX XXXXXXXX X% /
tmpfs XXXXXXXX XXXXXXXX XXXXXXXX X% /dev/shm
tmpfs XXXXXXXX XXXXXXXX XXXXXXXX X% /run
tmpfs XXXXXXXX XXXXXXXX XXXXXXXX X% /run/lock
tmpfs XXXXXXXX XXXXXXXX XXXXXXXX X% /sys/fs/cgroup
/dev/sda1 XXXXXXXX XXXXXXXX XXXXXXXX X% /var/lib/docker/overlay2/...
tmpfs XXXXXXXX XXXXXXXX XXXXXXXX X% /run/user/1000
This output shows the disk usage in 1K-blocks, which can be a bit hard to read. To make it more human-readable, we can use the -h option. The -h stands for "human-readable".
Now, type the following command and press Enter:
df -h
The output will be much easier to understand, showing sizes in units like KB, MB, GB, etc.:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 XXG XXG XXG X% /
tmpfs XXXM XXXK XXXM X% /dev/shm
tmpfs XXXM XXXM XXXM X% /run
tmpfs XXXM XXXK XXXM X% /run/lock
tmpfs XXXM XXXK XXXM X% /sys/fs/cgroup
/dev/sda1 XXG XXG XXG X% /var/lib/docker/overlay2/...
tmpfs XXXM XXXK XXXM X% /run/user/1000
The columns in the output are:
Filesystem: The name of the filesystem.Size: The total size of the filesystem.Used: The amount of space used on the filesystem.Avail: The amount of space available on the filesystem.Use%: The percentage of space used.Mounted on: The directory where the filesystem is mounted.
The / entry represents the root filesystem, which is where your operating system and most of your files are stored.
You can also check the disk usage of a specific directory by providing the directory path as an argument to df -h. For example, to check the disk usage of your home directory (~), you can use:
df -h ~
This will show you the disk usage of the filesystem where your home directory is located.
Practice using df -h to check the disk usage of different locations on the system. This command is a fundamental tool for monitoring disk space.
Click Continue to proceed to the next step.
Measure directory size with du -sh
In the previous step, you learned how to check the overall disk usage of filesystems using df. Now, let's learn how to measure the size of specific directories and files using the du command.
The du command stands for "disk usage". It estimates file space usage. By default, du will list the disk usage for each file and subdirectory within the specified directory, which can be quite verbose.
Let's try the basic du command in your current directory (~/project):
du
You will likely see output listing the size of the current directory and any subdirectories within it. The sizes are shown in blocks (usually 1KB blocks).
To get a summary of the total size of a directory and display it in a human-readable format, we use the -s (summary) and -h (human-readable) options together:
du -sh
This command will show you the total size of the current directory (~/project). The output will look something like this:
XXK .
The XXK represents the size of the directory in kilobytes. If the directory contained more files, you might see MB or GB.
You can also use du -sh to check the size of other directories. For example, to check the size of your home directory (~), type:
du -sh ~
The output will show the total size of your home directory:
XXM /home/labex
Let's create a small file to see how the size changes. Use the echo command to create a file named test_file.txt with some content:
echo "This is a test file." > test_file.txt
Now, run du -sh again in the ~/project directory:
du -sh
You might see a slightly larger size reported, depending on the size of the file you created.
The du -sh command is very useful for quickly finding out how much space a particular directory or file is consuming.
Click Continue to move on to the next step.
List partitions with cat /proc/partitions
In this step, you will learn how to view the disk partitions recognized by your Linux system. Disk partitions are divisions of a hard drive or other storage device.
Linux provides a virtual filesystem called /proc that contains information about the system and its processes. One of the files in this filesystem, /proc/partitions, contains information about the disk partitions.
You can view the content of this file using the cat command. The cat command is used to display the content of files.
Type the following command in your terminal and press Enter:
cat /proc/partitions
You will see output similar to this:
major minor #blocks name
8 1 XXXXXXXX sda1
Let's break down the output:
major: The major device number, which identifies the device driver.minor: The minor device number, which identifies the specific device or partition.#blocks: The size of the partition in 1KB blocks.name: The name of the partition. In this case,sda1likely represents the first partition on the first SCSI disk (or SATA disk, which is often treated as SCSI).
This output gives you a raw view of the partitions detected by the kernel. It's a quick way to see the basic partition layout of your system.
While cat /proc/partitions provides basic information, other commands like lsblk or fdisk -l offer more detailed and user-friendly views of disk devices and partitions, including their types and mount points. However, /proc/partitions is a simple and direct way to access this kernel-level information.
You have now learned how to check overall disk usage, measure directory sizes, and list disk partitions. These are essential skills for managing storage on a Linux system.
Click Continue to complete this lab.
Summary
In this lab, you learned how to check disk space sufficiency in Linux using several commands. You started by using the df -h command to display overall disk usage in a human-readable format, showing the total size, used space, available space, and usage percentage for each mounted filesystem. This is a fundamental step in understanding the general disk space situation on your system.
Next, you explored how to measure the size of specific directories using the du -sh command. This allows you to identify which directories are consuming the most disk space, which is helpful for troubleshooting and freeing up space. Finally, you learned how to list the partitions on your system by examining the contents of the /proc/partitions file using the cat command, providing insight into the underlying disk structure.



