Introduction
In this blog, we will discuss how to check the disk and file system’s available capacity, used and free with the Linux operating system. Linux distributions have df and du as built-in commands to check disk space usage. However, these commands are agnostic whether the disks are internal or external. Linux system administrators use these commands to monitor disk and file system utilization. df and du commands are compatible with all Linux shells. However, the below are the command examples run in the Bash shell.
df
The df command reports disk file system capacity utilization, including file system on the whole disk or partitions on the disks. The command outputs the details of the total, available, and used disk space file systems and mount points on all the disks. This command uses the following structure.
# df [OPTION]… [FILE]…
If no arguments are provided, it reports disk usage of all the mounted disk file systems in the 1K-block format.
# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 900208 0 900208 0% /dev
tmpfs 930464 18528 911936 2% /dev/shm
tmpfs 930464 9612 920852 2% /run
tmpfs 930464 0 930464 0% /sys/fs/cgroup
/dev/mapper/rhel-root 14034944 4626992 9407952 33% /
/dev/sda1 1038336 248008 790328 24% /boot
tmpfs 186092 9296 176796 5% /run/user/1000
We can use the option –human-readable or -h to make output human-readable format in bytes as a 1K-block format is complex for most users to understand.
# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 880M 0 880M 0% /dev
tmpfs 909M 19M 891M 2% /dev/shm
tmpfs 909M 9.4M 900M 2% /run
tmpfs 909M 0 909M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 14G 4.5G 9.0G 33% /
/dev/sda1 1014M 243M 772M 24% /boot
tmpfs 182M 9.1M 173M 5% /run/user/1000
This command also reports the usage of an individual mounted file system by providing the mount point as an argument.
# df /boot
Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 1038336 248008 790328 24% /boot
We can also check the total capacity used on the disk file systems with the argument –total.
# df -h --total
Filesystem Size Used Avail Use% Mounted on devtmpfs 880M 0 880M 0% /dev tmpfs 909M 19M 891M 2% /dev/shm tmpfs 909M 9.4M 900M 2% /run tmpfs 909M 0 909M 0% /sys/fs/cgroup /dev/mapper/rhel-root 14G 4.5G 9.0G 33% / /dev/sda1 1014M 243M 772M 24% /boot tmpfs 182M 9.1M 173M 5% /run/user/1000 total 19G 4.7G 14G 26% -
Inodes are data structures in the Linux file system that hold metadata of files or directories. That metadata includes file or directory location, name, ownership, size, timestamp, and permissions information. Each disk file system has its unique number of inodes and inode tables. Every file or directory created is assigned an individual inode. No new files or directories can be created if all inodes are consumed, even if disk space is available. We can run the df command with the option –inode or -i to check inode usage of the disk file systems.
# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on devtmpfs 225052 365 224687 1% /dev tmpfs 232616 44 232572 1% /dev/shm tmpfs 232616 784 231832 1% /run tmpfs 232616 17 232599 1% /sys/fs/cgroup /dev/mapper/rhel-root 7022592 124267 6898325 2% / /dev/sda1 524288 309 523979 1% /boot tmpfs 232616 51 232565 1% /run/user/1000
We can customize df command output using the argument –output to filter out only file system names, used, available space, and targeted mount points.
# df -h --output=source,used,avail,target
Filesystem Used Avail Mounted on devtmpfs 0 880M /dev tmpfs 19M 891M /dev/shm tmpfs 9.4M 900M /run tmpfs 0 909M /sys/fs/cgroup /dev/mapper/rhel-root 4.5G 9.0G / /dev/sda1 243M 772M /boot tmpfs 9.1M 173M /run/user/1000
du
Another useful tool is the disk usage command to check disk space used by files and directories. The du command provides information about the file or directory size on the file system used. Below is the du command structure.
$ du [OPTION]… [FILE]…
The du command lists the files and directory sizes. It also reports the summary of the total usage of all files and directories. We are running the du command in the directory /home/test .
# du
8 ./firewalld/zones 16 ./firewalld 4 ./yum.repos.d 4 ./libvirt/qemu/networks 4 ./libvirt/qemu 184 ./libvirt 224 .
The du command with –human-readable or -h option reports, directories sizes in current directory or within specified directory the human-readable format in KiloByte.
# du -h .
# du -h /home/test
8.0K /home/test/firewalld/zones 16K /home/test/firewalld 4.0K /home/test/yum.repos.d 4.0K /home/test/libvirt/qemu/networks 4.0K /home/test/libvirt/qemu 184K /home/test/libvirt 224K /home/test/
Further, the du command with option –summarize or -s with -h gives us human-readable summary usage of directories.
# du -sh
224K .
# du -sh /home/test
224K /home/test
We can get the size of each directory and file individually listed under the directory path with the option of –all or -a .
# du -ah
4.0K ./hostname 4.0K ./locale.conf 12K ./kdump.conf 4.0K ./firewalld/firewalld.conf 4.0K ./firewalld/lockdown-whitelist.xml 4.0K ./firewalld/zones/public.xml 4.0K ./firewalld/zones/public.xml.old 8.0K ./firewalld/zones 16K ./firewalld 4.0K ./yum.repos.d/redhat.repo 4.0K ./yum.repos.d 4.0K ./libvirt/libvirt-admin.conf 4.0K ./libvirt/libvirt.conf 20K ./libvirt/libvirtd.conf 4.0K ./libvirt/virtlockd.conf 4.0K ./libvirt/virtlogd.conf 20K ./libvirt/virtproxyd.conf 12K ./libvirt/virtnetworkd.conf 12K ./libvirt/virtinterfaced.conf 12K ./libvirt/virtnodedevd.conf 12K ./libvirt/virtnwfilterd.conf 4.0K ./libvirt/qemu-lockd.conf 32K ./libvirt/qemu.conf 12K ./libvirt/virtqemud.conf 12K ./libvirt/virtsecretd.conf 12K ./libvirt/virtstoraged.conf 4.0K ./libvirt/qemu/networks/default.xml 4.0K ./libvirt/qemu/networks 4.0K ./libvirt/qemu 184K ./libvirt 224K .
Using a file or directory name as an argument, we can also check the size of the file and directory.
# du -h hostname
4.0K hostname
In addition, we have –time as an option that provides the last modification timestamp and files or directory sizes.
# du -ah --time
4.0K 2023-04-18 15:58 ./networks/default.xml 4.0K 2023-04-18 16:09 ./networks 4.0K 2023-04-18 16:09 .
We can exclude files or directories by filtering specific patterns using the option –exclude.
# du -ah --exclude="*.conf"
4.0K ./hostname 4.0K ./firewalld/lockdown-whitelist.xml 4.0K ./firewalld/zones/public.xml 4.0K ./firewalld/zones/public.xml.old 8.0K ./firewalld/zones 12K ./firewalld 4.0K ./yum.repos.d/redhat.repo 4.0K ./yum.repos.d 4.0K ./libvirt/qemu/networks/default.xml 4.0K ./libvirt/qemu/networks 4.0K ./libvirt/qemu 8.0K ./libvirt 28K .
Conclusion
In conclusion, both df and du are handy commands used by Linux Admins to keep track of disk usage. The df command checks the disk space of the whole file system. The du command checks specific file and directory sizes stored on the file system. Both commands are very useful for Linux administrator track disk capacity and utilization.