How to check the disk space in Linux distributions? πŸ‘‹

Checking disk space is an important task in Linux based machines because we may run heavy data intensive tasks that creates logs and without proper log rotation the disk will be filled up in just couple of days. To make sure we handle all the log rotation properly it is mandatory to check the space available in the disk at any given point in time.

Introduction

UNIX/Linux based OS evolved for around 45-50 years at the time of writing this article. So we have multiple terminal based tools to check the disk space. All does the same job but the way they represent differs.

df command

df stands for “disk free”. This command will query the kernel and get the free space in the all the mounted disk in return.

$ df -h //shows the disk space in human readable format.
$ df -a //disk usage by file system even if it is 0.
$ df -T //disk usage in each block's filesystem type.
$ df -i //uses and free inodes.

You can also limit the reported fields shown in the df output. Available fields are:

source β€” the file system source

size β€” total number of blocks

used β€” spaced used on a drive

avail β€” space available on a drive

pcent β€” percent of used space, divided by total size

target β€” mount point of a drive

An example is shown below: df -H –output=size,used,avail

df -h:

df -h

df -a:

df -a

df -T:

df -T

df -i:

df -i

du command

du shows the disk usage of files and the folders by default in kilobyte but we can pass arguments to get output in megabytes, gigabytes, etc.

$ du -h //shows the disk space in human readable format.
$ du -a //disk usage of all the files.
$ df -s //disk usage of specific file or directory.

du is used to get the space occupied by all the files in the machine. Using it alone in storage optimized machines would do no good. So it is always good to use it with other comments. For example pipe (|) the output to the grep command and just find the memory occupied by a specific file or directory. Even this could be used to find a file or directory in the Linux machines.

ls -alh vs ls -lh vs ls -al commands

ls -alh, ls -lh and ls -al all are used to find the disk usage of files in a particular directory. Here is an example:

ls -alh ls -lh ls -al

stat command

stat <file/directory> displays the size and other stats of a file/directory or a filesystem.

ls -alh ls -lh ls -al

fdisk -l command

fdisk -l shows disk size along with disk partitioning information.

ls -alh ls -lh ls -al

References

[1] https://opensource.com/article/18/7/how-check-free-disk-space-linux

[2] https://www.linuxfoundation.org/blog/blog/classic-sysadmin-how-to-check-disk-space-on-linux-from-the-command-line