10 different methods to check disk space in Linux

Advertisement

In this article I will share different commands and methods to check disk space in Linux. Most of these commands and methods should be available on most Linux distros. You can also utilise these commands to create a script to monitor disk size usage and create alarms or send mails (this would need a mail server).

 

1. Check partition size using df command

df is one of the most used command to check space of available partitions on the Linux setup. You can use df wih -Th to print the partition type and the partition size in human readable format. This command will show you the total available, used and free space per partition.

Check disk space in Linux using 10 different CMDs

You can also use other arguments such as -i to print the available, used and free inode count per partition:

Check disk space in Linux using 10 different CMDs

You can check the man page of df command for other supported arguments to check disk space in different formats.

 

2. Check disk space using fdisk utility

fdisk is another very handy utility for system administrators. fdisk is a user interactive program mostly used for creation and manipulation of partition tables such as creating, deleting, modifying partitions. But we can also use this utility to list the available disks connected to the Linux system along with the respective disk size.

Here is a snippet of output from command "fdisk -l"
Check disk space in Linux using 10 different CMDs

As you can see from the output, I have a disk /dev/sda with a size of 15GB. The output also shows more details about this disks wherein it contains two partitions /dev/sda1 and /dev/sda2.

The output of fdisk -l can be quiet long so if you just wish to check the disk space then you can use this command:

Advertisement
~]# fdisk -l | awk '$1=="Disk" && $2 ~ /^\/dev\/.*/ {print $2 $3 $4}'
/dev/sda:15GiB,
/dev/sdb:8GiB,
/dev/mapper/rhel-root:14GiB,
/dev/mapper/rhel-swap:512MiB,

Here I have combined fdisk with awk to only print required sections. So I have two disks with 15GB and 8GB each, while I have two LVM partitions which are most likely part of these disks with a size of 14GB and 512MB.

 

3. Check disk space using parted utility

parted is another alternative to fdisk and is also used for manipulating disk partitions. It is useful for creating space for new operating systems, reorganising disk usage, and copying data to new hard disks.

Similar to fdisk, we can use parted -l to list all the available disks along with their respective size:

Check disk space in Linux using 10 different CMDs

If you compare the output of parted with fdisk, here we only get the available disk and their size and the LVM details are missing which is a good thing as we only wanted to check the disk space.

We can also use awk with parted to further only print required output:

~]# parted -l | awk '$1=="Disk" && $2 ~ /^\/dev\/.*/ {print $2 $3 $4}'
/dev/sda:16.1GB
/dev/sdb:8590MB

 

4. Check file size using du command

du is another wonderful utility to check the size of files recursively in Linux. It can also summarize disk usage of the set of files, recursively for directories. du is a very versatile tool and supports multiple arguments which you can use based on your requirement. Some of the most used arguments are:

       -a, --all
              write counts for all files, not just directories

       -c, --total
              produce a grand total

       -d, --max-depth=N
              print  the total for a directory (or file, with --all) only if it is N or fewer levels below the com‐
              mand line argument;  --max-depth=0 is the same as --summarize

       -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

       -s, --summarize
              display only a total for each argument

       -t, --threshold=SIZE
              exclude entries smaller than SIZE if positive, or entries greater than SIZE if negative

Here are some example demonstrating different scenarios to check file system size using du command:

 

EG-1: Check size of all the files under a partition recursively

I have a separate partition for /boot:

Advertisement
~]# df -h /boot/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       488M  123M  330M  28% /boot

So let's check the size of all the files under /boot using du -h /boot/*
Check disk space in Linux using 10 different CMDs

 

EG-2: Print total summary of size of files in a partition or directory

We can use du -c to print a total or summary of all the file size under provided directory or partition.

~]# du -sch /etc/iscsi/*
4.0K    /etc/iscsi/initiatorname.iscsi
16K     /etc/iscsi/iscsid.conf
20K     total

So the total file size under /etc/iscsi directory is 20K. You can also use this command under root i.e. du -sch /* to get a summary and total size of all the files under /

 

EG-3: Sort the output based on file size

By default the du command will print the size of the file based on the first available directory or sub-directory. So the output is not sorted and it can be a tedious task to go through the long list. We can combine du with sort command do sort the output based on the file size.

Check disk space in Linux using 10 different CMDs

Here we have used du with sort -hr where -h is used to compare human readable numbers such as 2K, 1G, 4M etc while -r is used to reverse the order of search.

 

EG-4: Print file size larger than specified size

By default du will print the size of every file found under a partition or directory. We can add a threshold to print files higher than a certain size.

For example here we are printing the files with size higher than 1MB under /var/log

~]# du -ach -t 1M /var/log/* | sort -hr
19M     total
8.9M    /var/log/anaconda
4.2M    /var/log/anaconda/lvm.log
3.2M    /var/log/anaconda/journal.log
1.9M    /var/log/dnf.librepo.log
1.6M    /var/log/audit/audit.log
1.6M    /var/log/audit

For more list of supported options check the man page of du command.

 

5. Check disk size using lsblk command

lsblk lists information about all available or the specified block devices. The lsblk command reads the sysfs filesystem and udev db to gather information. If the udev db is not available or lsblk is compiled without udev support than it tries to read LABELs, UUIDs and filesystem types from the block device.

We can use lsblk with -o LIST or --output LIST to get desired columns based on the value of LIST. Use lsblk --help to get a list of all supported columns.

We will use name, fstype, size, mountpoint LIST for our example. Although using only size and name was enough but to give more detailed output I am using additional list options.

Check disk space in Linux using 10 different CMDs

 

6. Print disk size using blockdev

We can use blockdev command to print a report for the specified device. It is possible to give multiple devices. If none is given, all devices which appear in /proc/partitions are shown. Note that the partition StartSec is in 512-byte sectors.

Check disk space in Linux using 10 different CMDs

You can also use following commands:

# returns size in bytes.
# blockdev --getsize64 /dev/sda

# returns size in 512-byte sectors.
# blockdev --getsz /dev/sda

 

7. Check disk size and details using lshw command

lshw is a small tool to extract detailed information on the hardware configuration of the machine. It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc. on DMI-capable x86 or IA-64 systems and on some PowerPC machines.

By default lshw will give you a huge output, we can limit that by only printing the information for a specified class such as "disk". To get the list of attached disks and their details such as size we will use lshw -c disk.

Here is an output snippet from my Linux node:

Check disk space in Linux using 10 different CMDs

We can further improve the output using lshw with grep:

~]# lshw -c disk | grep -E "logical name|size:"
       logical name: /dev/sda
       size: 15GiB (16GB)
       logical name: /dev/sdb
       size: 8GiB (8589MB)

 

8. Checking disk size from the system logs

We can also use system logs such as boot logs using dmesg or journalctl -b and try to search for respective disk to get more information. The catch is that you should know the disk name unlike other methods which we discussed where we were able to check size of all the available disks without prior knowledge of disk name.

For example, here I am searching for all instance of sda disk in boot logs using dmesg:

Check disk space in Linux using 10 different CMDs

Similarly we can search for any other disk, such as sdb:

 ~]# dmesg | grep sdb
[    3.230296] sd 3:0:0:0: [sdb] 16777216 512-byte logical blocks: (8.59 GB/8.00 GiB)
[    3.230304] sd 3:0:0:0: [sdb] Write Protect is off
[    3.230306] sd 3:0:0:0: [sdb] Mode Sense: 00 3a 00 00
[    3.230316] sd 3:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[    3.238804]  sdb: sdb1
[    3.239698] sd 3:0:0:0: [sdb] Attached SCSI disk

You may also try grepping blocks in dmesg output which can list the available disk with their size:

 ~]# dmesg | grep blocks
[    3.228462] sd 2:0:0:0: [sda] 31457280 512-byte logical blocks: (16.1 GB/15.0 GiB)
[    3.230296] sd 3:0:0:0: [sdb] 16777216 512-byte logical blocks: (8.59 GB/8.00 GiB)

 

9. Check disk size using lsscsi command

lsscsi uses information in sysfs (Linux kernel series 2.6 and later) to list SCSI devices (or hosts) currently attached to the system. Many non-SCSI storage devices (but not all) used the SCSI subsystem in Linux. In lsscsi version 0.30 support was added to list NVMe devices.

We can use lsscsi --size to list all the connected storage devices along with their size as shown below:

~]# lsscsi --size
[0:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr0        -
[1:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr1        -
[2:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sda   16.1GB
[3:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sdb   8.58GB

 

10. Print disk size using sfdisk

sfdisk is again an alternative to fdisk and parted utility. One of the major difference between fdisk and sfdisk is that sfdisk reads and writes partition tables, but is not interactive like fdisk or cfdisk (it reads input from a file or stdin). It's generally used for partitioning drives from scripts or for partition table backup and recovery.

But it can also be used to check the disk size using sfdisk -l, sample output:

Check disk space in Linux using 10 different CMDs

 

Summary

In this article we explored different commands and methods which can be used to check disk size (used/available/total) in Linux. You can easily utilize most of these commands and methods into a script to regularly monitor the available disk space and raise alarm when threshold is reached. There are some more commands such as udisks, hwinfo which can be used to collect similar information but they are distro dependent and may not be available with all the Linux distributions hence I have skipped them.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment