Contents

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:

1
2
3
4
5
6
7
8
> df
Filesystem                   1K-blocks      Used     Available Use% Mounted on
devtmpfs                     402668         0        402668   0%    /dev
tmpfs                        419116         0        419116   0%    /dev/shm
tmpfs                        419116         10876    408240   3%    /run
tmpfs                        419116         0        419116   0%    /sys/fs/cgroup
/dev/mapper/rhel_rhel8-root  30320164       1927196  28392968 7%    /
/dev/sda1                    1038336        179520   858816   18%   /boot

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:

1
2
3
4
> df -T -t xfs -h
Filesystem                  Type  Size  Used Avail Use% Mounted on
/dev/mapper/rhel_rhel8-root xfs    29G  1.9G   28G   7% /
/dev/sda1                   xfs  1014M  176M  839M  18% /boot

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:

1
2
3
4
> du
0	    ./testdir
1024	./myfiles
1050624	.

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:

1
2
3
4
> du -csh /home/ /root
1.1G	/home/
28K	    /root
1.1G	total.

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.