Managing disk space is crucial for maintaining a healthy and efficient Linux system. Whether you’re troubleshooting issues or planning for future storage needs, regularly checking the available disk space is a fundamental task. In this article, we’ll explore various commands to help you determine the disk space on your Linux system.
Using df Command
The df
command is a simple and effective tool to display disk space usage. Open a terminal and type the following command:
df -h
This command provides a human-readable output, summarizing the disk space usage across different filesystems:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 8.2G 10G 46% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/sdb1 100G 25G 75G 25% /home
/dev/sdc1 250G 180G 70G 73% /data
In this example:
- Filesystem: The name of the filesystem.
- Size: The total size of the filesystem.
- Used: The amount of space used.
- Avail: The available space.
- Use%: The percentage of space used.
- Mounted on: The directory where the filesystem is mounted.
This output gives a quick overview of the disk space usage on the system, making it easy to identify how much space is used and available on each mounted filesystem.
Human-Readable Output
To display disk space in a more human-readable format (e.g., in kilobytes, megabytes, or gigabytes), use the -h
option:
df -h
Specific Filesystem
To check the disk space of a specific filesystem (e.g., root filesystem):
df -h /dev/sda1
Display Inodes
To display information about inodes (data structures on a filesystem), use the -i
option:
df -i
Exclude Specific Filesystems
To exclude specific filesystem types from the output, use the -x
option. For example, excluding the tmpfs filesystem:
df -h -x tmpfs
Display Total Disk Space
To display the total disk space, use the --total
option:
df --total
Include Filesystem Type
Include the filesystem type in the output for better clarification:
df -T
Display Filesystem Usage for a Directory
To check disk space usage for a specific directory (e.g., /home):
df -h /home
Custom Output Format
For more flexibility, you can use the --output
option to customize the output. For example, to display only the filesystem and used space:
df --output=source,used
Show Available Inodes
To display the number of free and used inodes, use the -i
option:
df -i
Show Filesystem Type and Size in Blocks
To display the filesystem type and size in 1K blocks
df -h -t ext4 --output=source,size
These various options allow you to customize the df
command based on your specific requirements, providing detailed insights into disk space usage on your Linux system.
Checking Disk Space Usage using the du
command
The du
command in Linux is used to estimate the space used by directories and files. It is particularly useful for finding out which directories or files consume the most space on your filesystem. Here are several ways to use the du
command:
The basic usage of du
without any options shows the disk usage of the current directory:
du
Here’s an example of the du
command output:
4.0K ./Documents
8.0K ./Downloads
16K ./Pictures
24K ./Music
36K ./Videos
48K ./Desktop
4.0K ./Templates
76K ./total
In this example:
- Each line represents the disk usage of a specific directory or file.
- The first column indicates the disk space used in kilobytes (KB).
- The second column displays the path of the directory or file.
Human-Readable Output:
To display disk usage in a human-readable format (e.g., in kilobytes, megabytes, or gigabytes), use the -h
option:
du -h
Disk Usage of a Specific Directory:
To check the disk usage of a specific directory (e.g., /var/www):
du -h /var/www
Total Disk Usage of a Directory:
To display the total disk usage of a directory, including its subdirectories:
du -h --max-depth=1 /path/to/directory
Sorting Output by Size
To sort the output by size, you can use the --sort
option. For example, to sort in descending order:
du -h --max-depth=1 /path/to/directory | sort -hr
Display Only Total Disk Usage
To display only the total disk usage of a directory, use the -s
option:
du -h -s /path/to/directory
Excluding Certain Directories
To exclude specific directories from the output, use the --exclude
option. For example, to exclude the “backup” directory:
du -h --exclude=backup /path/to/directory
Display Inodes:
To display the number of used and free inodes along with disk usage:
du -h --inodes /path/to/directory
Summary of Disk Usage
To get a summary of disk usage in a directory, showing total disk space used and available:
du -sh /path/to/directory
Analyzing Disk Usage in a Filesystem:
To analyze disk usage in a filesystem, sorting directories and files by size:
du -h --max-depth=1 / | sort -hr
These examples demonstrate how the du
command can be used to analyze disk usage in different scenarios, making it a powerful tool for managing storage on a Linux system.
Displaying Disk Usage Statistics with Disk Usage Analyzer
If you prefer a graphical representation, Linux systems often come with a Disk Usage Analyzer tool. You can launch it from the command line by typing:
baobab
This tool provides an interactive, graphical representation of disk space usage, making it easier to identify large files and directories.
Monitoring Real-time Disk Space Usage with iostat
To monitor real-time disk space usage and I/O statistics, the iostat
command can be valuable. Install it using:
sudo apt-get install sysstat
After installation, run:
iostat
This command displays CPU statistics, I/O information, and disk utilization.
Identifying Large Files with find Command
Locating large files is essential for freeing up disk space. The find
command can help in this regard. For example, to find files larger than 1GB in the home directory:
find /home -type f -size +1G
This command searches for files larger than 1GB within the specified directory.
Conclusion
Regularly checking disk space is vital for maintaining the performance and reliability of your Linux system. The commands mentioned above provide valuable insights into disk space usage, enabling you to take proactive measures to manage storage effectively. Incorporate these commands into your routine system maintenance to ensure a well-organized and efficient Linux environment.