
Managing disk space is a critical responsibility for Linux system administrators and users. A full disk can degrade system performance, cause services to fail, and can even prevent users from logging in or accessing essential directories. Whether you manage a local machine, a bare metal server, or a cloud server such as a Vultr Compute Instance, understanding disk usage helps ensure system stability and avoid unexpected outages.
This article explains how to check disk space and usage on a Linux system using built-in command-line tools. You will learn how to identify large files, analyze mounted file systems, and troubleshoot disk-related issues efficiently.
Linux includes several essential command-line tools that help monitor and manage disk space efficiently. These tools offer insight into the amount of storage used, available space, and which files or directories are consuming the most disk space. Regular use of these tools helps prevent system slowdowns, service interruptions, and data loss due to full partitions.
The df command provides a quick overview of disk usage across all mounted file systems. Use it to identify partitions with low free space and act before system performance is degraded.
df Command SyntaxThe basic syntax for the df command is as follows:
Within the command:
df command.df will display the disk space for all mounted file systems.df Command Optionsdf CommandTo display the disk usage of all mounted file systems in a human-readable format.
Your output should be similar to the one below:
In the above output you have:
To check disk usage of a specific mount point such as /mnt, use the below command.
Your output should be similar to the one below:
This command displays the disk space information of /dev/vdb1 partition mounted on /mnt directory.
To check the disk space information for a specific file system type, such as ext.
This command displays disk usage only for file systems of type ext4 in a human-readable format.
Your output should be similar to the one below:
In the above output, both /dev/vda2 and /dev/vdb1 are ext4 partitions. The Size, Used, and Avail columns help monitor disk usage for these specific file systems.
To exclude certain file system types, such as tmpfs, from the output.
Your output should be similar to the one below:
To include the type of each file system in the output.
This command displays disk space information along with the type of each file system, helping identify the file system format used for each mount point.
Your output should be similar to the one below:
To display inode usage, which is useful for monitoring the number of files and directories.
Your output should be similar to the one below:
In the above output:
The du (disk usage) command in Linux is used to estimate and display the disk space used by files and directories. It's an essential tool for monitoring disk usage, helping you identify large files or directories that may need to be cleaned up to free space.
du Command SyntaxThe basic syntax for the du command is as follows:
Within the command:
du command.du shows the usage of the current directory.du Command Optionsdu CommandTo check the disk usage information of a specific directory in a human-readable format, use the -sh options.
It's recommended to use sudo with this command, as some files or directories may require elevated permissions.
In the above command:
-s: Shows only the total disk usage (summary).-h: Prints sizes in a human-readable format.To include hidden files (those starting with a dot) in the disk usage report, use the -a option.
In this command:
-a: Includes all files, not just directories, in the output.This is useful for identifying the disk usage of all files, including configuration files that are typically hidden.
To exclude certain files or directories from the disk usage calculation, use the --exclude option.
In this command:
--exclude='*.log': Excludes all files with the .log extension from the report.This is useful for ignoring temporary or log files that are not relevant to the analysis.
To list the disk usage of all subdirectories within a specific directory (for example, /var), use the --max-depth=1 flag. This limits the display to the top-level subdirectories.
Your output should be similar to the one below:
To sort the directories by disk usage, use the sort command in combination with du. For example, to sort the subdirectories of /var from largest to smallest.
Your output should be similar to the one below:
In this output, the directories are sorted in descending order by their disk usage, with the largest directory listed first.
Use the find command along with du to locate large files in a directory. For example, to find all files larger than 50MB within the /var directory.
Your output should be similar to the one below:
This command lists all files in /var larger than 50 MB. The output shows the file size, permissions, and last modification date of each file.
You have now learned how to effectively manage disk space on your Linux system using essential command-line tools like df, du, and find. By monitoring disk usage, you can identify large files, analyze mounted file systems, and prevent potential system slowdowns or outages due to full disks. With the commands and examples provided in this article, you are equipped to keep your system running and avoid disk-related issues.
0 Comments
Be the first to comment and share your perspective with the community.