How to check disk space on linux CLI (Detailed)

This is a detailed guide on how to check free disk space on Linux Based Operating Systems.

This command will show you the actual size of the disk / HDD or Virtual Disk / Bunch of disks you have on your Linux machine, the other commands are mainly for checking the usage of the files on the filesystem

root@ubuntu:~# fdisk -l 2> /dev/null | grep Disk | grep -v identifier
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disklabel type: gpt

## Or you could do

root@ubuntu:~# fdisk -l | egrep 'Disk.*bytes' | awk '{ sub(/,/,""); sum +=$3; print $2" "$3" "$4 } END { print" Total“"; print "total: " sum " GB"}'
/dev/vda: 100 GiB
 Total“
total: 100 GB

or a million and one other ways to filter the results

basically it's show in fdisk -l

You can just read it then.

root@ubuntu:~# fdisk -l
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 848AD1B1-1D5F-401B-A434-EA84F4486685

Device      Start       End   Sectors  Size Type
/dev/vda1  227328 209715166 209487839 99.9G Linux filesystem
/dev/vda14   2048     10239      8192    4M BIOS boot
/dev/vda15  10240    227327    217088  106M EFI System

Partition table entries are not in disk order.

This will display the size of each partition, however it is shown by the number of 1K bytes

root@ubuntu:~# df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev             2006700       0   2006700   0% /dev
tmpfs             403924    7216    396708   2% /run
/dev/vda1      101445540 2285736  99143420   3% /
tmpfs            2019612       0   2019612   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs            2019612       0   2019612   0% /sys/fs/cgroup
/dev/vda15        106858    3668    103190   4% /boot/efi
tmpfs             403920       0    403920   0% /run/user/0

Adding the -h flag will display this in a human readable format, this then shows the size of each partition.

root@ubuntu:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           395M  7.1M  388M   2% /run
/dev/vda1        97G  2.2G   95G   3% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/vda15      105M  3.6M  101M   4% /boot/efi
tmpfs           395M     0  395M   0% /run/user/0
root@ubuntu:~# df -h /dev/vda
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev

You can run this against each filesystem to hone in on that particular file system

root@ubuntu:~# df -h --o
Filesystem     Type     Inodes IUsed IFree IUse%  Size  Used Avail Use% File Mounted on
udev           devtmpfs   490K   630  490K    1%  2.0G     0  2.0G   0% -    /dev
tmpfs          tmpfs      494K  2.1K  491K    1%  395M  7.1M  388M   2% -    /run
/dev/vda1      ext4        13M  110K   13M    1%   97G  2.2G   95G   3% -    /
tmpfs          tmpfs      494K     1  494K    1%  2.0G     0  2.0G   0% -    /dev/shm
tmpfs          tmpfs      494K     5  494K    1%  5.0M     0  5.0M   0% -    /run/lock
tmpfs          tmpfs      494K    18  494K    1%  2.0G     0  2.0G   0% -    /sys/fs/cgroup
/dev/vda15     vfat          0     0     0     -  105M  3.6M  101M   4% -    /boot/efi
tmpfs          tmpfs      494K    10  494K    1%  395M     0  395M   0% -    /run/user/0

Applying the flag –output will add all the availiable fields to display, the following are valid options for the flag and can be listed

Display NameValid field Description
Filesystem source The source of the mount point, usually a device.
1K-blocks size Total number of blocks.
Used used Number of used blocks.
Available
avail
Number of available blocks.
Mounted on
target
The mount point.
Use% pcent Percentage of USED divided by SIZE.

So here are some examples

root@ubuntu:~# df -h --output=source,used,avail
Filesystem      Used Avail
udev               0  2.0G
tmpfs           7.1M  388M
/dev/vda1       2.2G   95G
tmpfs              0  2.0G
tmpfs              0  5.0M
tmpfs              0  2.0G
/dev/vda15      3.6M  101M
tmpfs              0  395M

You can also use the du command to get an overall usage of your system replace the directory to whichever directory you wish to get a summary of

root@ubuntu:~# du -sh /
2.2G    /
root@ubuntu:~# du -sh /etc
7.1M    /etc
root@ubuntu:~# du -sh /var
711M    /var

## Without a summary you get the directories listed and their value, the summary flag -s basically adds them up
root@ubuntu:~# du -h /var/www/html/ajax
12K     /var/www/html/ajax/min
60K     /var/www/html/ajax

## this would list the top 10 directories in /etc 
root@ubuntu:~# du -a /etc | sort -n -r | head -n 10
7256    /etc
1108    /etc/apparmor.d
692     /etc/apache2
664     /etc/ssl
632     /etc/ssl/certs
588     /etc/ssh
568     /etc/apache2/mods-available
544     /etc/ssh/moduli
516     /etc/apparmor.d/cache
416     /etc/apparmor.d/abstractions

## or the same in var
root@ubuntu:~# du -a /var | sort -n -r | head -n 10
727408  /var
376180  /var/lib
227876  /var/lib/mysql
176616  /var/www
176612  /var/www/html
115320  /var/www/html/test
110696  /var/lib/apt
110668  /var/lib/apt/lists
102804  /var/cache
93352   /var/cache/apt

If you are new to the world of Linux, an avid Linux enthusiast or a student why not try our 0.99p per month Linux VPS.

Simply click on the screen shot below to find out more or navigate to https://piggybank.cloud

Thank you for reading and please feel free to leave any feedback.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s