Disk Space Analysis in Linux
1. Overview
One of the most common administrative tasks while using Linux is finding out the disk space usage in our system. The most common command-line utilities to find the free disk space usage are the df (Disk Free) and du (Disk Usage commands).
In this tutorial, we will have a look at the common scenarios and usage of the du and df commands.
2. Common Usage Scenarios
We will be using the df and du commands interchangeably based on the information we are seeking, to check the amount of space used and free on our filesystem and partitions the df command is used additionally to check for space usage on a directory level the command du is used.
3. Checking File System Disk Usage
The df command displays usage by the various disk in the system with various information like the filesystem name, the free space in 1k blocks size, used space, available space, and the mounted directory.
Using the df command executed without any arguments:
|
|
The above output gives various information regarding the type of the filesystem, the total blocks used and available and the usage percentage of the total disk size.
The block size used to calculate the space the 1k-blocks header in the output specifies that each block in the system is considered to be 1000 bytes, to check usage in 1024 byte blocks we use the df -k command.
3.1. Common Options
- -a : To include all filesystems accessible and inaccessible file systems.
- -h : Prints the summary in a human-readable size format Bytes, Kilobytes, Gigabytes, etc.
- -t : Limit out to the filesystem of the supplied type.
- -T: Print filesystem type.
3.2. Extended Usage Using Common Options
Using some common options we print the disk space usage of all the xfs filesystem partitions in a human-readable format:
|
|
We use the ‘-T’ to show the filesystem type in the output and restrict the records to only size records for partition type xfs in a human-readable format.
4. Checking Directory Usage
The du command helps to find the disk usage by files and directory in the system. executing the du command without any argument on a directory gives the space used by the directory followed by the name of the directory, here the period ( “.”) specifies the current directory:
|
|
4.1. Common Options
- -c: Produces a total of the disk space used of all directories in the command input.
- -s: Produces a disk space summary of all the nested directories in a single entry.
- -h: Prints file Prints the summary in a human-readable size format Bytes, Kilobytes, Gigabytes, etc.
4.2. Extended Usage Using Common Options
Using some of the common options of the du command we print the disk space usage of the /home and /root directories in the system with:
|
|
5. Conclusion
In this article, we saw how to use the df and du Linux commands to check filesystem and directory usage, we can use these commands to manually verify and check disk usage or use them inside a script.