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.
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 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
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 -lh and
ls -al all are used to find the disk usage of files in a particular directory. Here is an example:
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.