Quick summary ↬ 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.
|
|
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 -a
:
df -T
:
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
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:
stat command
stat <file/directory> displays the size and other stats of a file/directory or a filesystem.
fdisk -l command
fdisk -l shows disk size along with disk partitioning information.
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