Introduction
Knowing how to check disk space in Linux is essential for maintaining system health and stability. It prevents performance issues, application failures, and potential data loss caused by low disk space.
The following text elaborates on different ways to check disk space in Linux.
Prerequisites
- A Linux system (this tutorial uses Ubuntu 22.04).
- Access to the terminal.
- A user account with sudo or root privileges.
Disk Space Check in Linux: 5 Methods
Performing disk space checks in Linux ensures enough storage is available for system operations and application usage. The following text presents common ways to check disk space in Linux.
Method 1: Check Disk Space via df Command
The df
(disk free) command lets you check disk space in Linux and shows the amount of space taken up by different drives. Run the following:
df
By default, df
displays values in 1-kilobyte blocks.
The output has several columns:
- Filesystem. The structure used to organize data. This includes physical hard drives, logical (partitioned), and virtual or temporary drives.
- 1K-blocks. The filesystem size displayed as 1 kilobyte blocks.
- Used. The amount of space used on each filesystem.
- Available. The amount of unused (free) space on the filesystem.
- Use%. The percentage of the disk used.
- Mounted on. The directory where the filesystem is located.
Note: Learn how to preallocate space for a file using the fallocate command.
Display Usage in Megabytes and Gigabytes
To modify the df
command and display disk usage in a more human-readable format, add the -h
option:
df -h
This displays the size in megabytes (M) and gigabytes (G).
Display a Specific Filesystem
The df
command can also display a specific filesystem. For example, run the following to show the usage on the primary hard drive:
df -h /
Note: The df
command only targets a full filesystem. Even if you specify an individual directory, df
reads the space of the whole drive.
Display Filesystems by Type
Filesystem types define how data is organized, stored, and accessed on a storage device. There are many filesystem types, each designed for a specific purpose.
For example, to list filesystems with the ext4 type, use the command:
df -ht ext4
The command lists drives with the ext4 type in human-readable format. This type is a widely used filesystem with features like journaling and support for large file sizes and partitions.
Method 2: Check Disk Space via the du Command
The du
command displays disk usage for individual directories in Linux. Use it to display the amount of space used by the current directory:
du
Like the df
command, du
can be human-readable:
du -h
It prints the current directory contents and how much space they're using in kilobytes, megabytes, and gigabytes.
Method 3: Check Disk Space via the pydf Command
pydf
is a Python-based command that provides a colorful and human-readable output of disk space usage on Linux. It is designed as an alternative to the standard df
command. Run the following command to check disk space:
pydf
Method 4: Check Disk Space via the fdisk -l Command
While fdisk
focuses on displaying disk partition details. It includes details about disk sizes and provides general information about disk capacity. Run the following command to check disk space:
sudo fdisk -l
Method 5: Check Disk Space via the lsblk Command
The lsblk
command lists information about block devices, including disks and their partitions. To test the tool, run the following:
lsblk
While lsblk
does not provide detailed disk usage info, the output shows a hierarchical view of block devices, including their names, major and minor numbers, sizes, read-only statuses, types, and mount points.
To determine the overall unallocated space on the drive, sum up the sizes of all partitions and then compare the total with the drive's total size.
Conclusion
This article explained how to check disk space in Linux using five different methods.
Next, learn how to use the fsck command to run a filesystem check as preventive maintenance or when there is an issue with your system.