There are many ways to evaluate disk space usage on Linux, and this post examines a number of commands that allow you to view used and available disk space.
Using the df (disk free) command
The df command stands for “disk free” and, as that name suggests, it focuses on how much free disk space is used and how much is available. By default, the command reports disk usage in kilobytes.
# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 4096 0 4096 0% /dev
tmpfs 1937768 0 1937768 0% /dev/shm
tmpfs 775108 1712 773396 1% /run
efivarfs 64 23 37 39% /sys/firmware/efi/efivars
/dev/sda3 13974528 11385772 1873284 86% /
/dev/loop0 106496 106496 0 100% /var/lib/snapd/snap/core/16928
/dev/loop1 8704 8704 0 100% /var/lib/snapd/snap/gping/13
tmpfs 1937768 16 1937752 1% /tmp
/dev/sda2 996780 250032 677936 27% /boot
/dev/sda1 613160 19484 593676 4% /boot/efi
/dev/sda3 13974528 11385772 1873284 86% /home
tmpfs 387552 132 387420 1% /run/user/1000
tmpfs 387552 44 387508 1% /run/user/1001
Used with the -h option, df reports in the “human-readable” format, adjusting the data to report in megabytes, kilobytes or gigabytes as appropriate.
$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 757M 1.7M 756M 1% /run
efivarfs 64K 23K 37K 39% /sys/firmware/efi/efivars
/dev/sda3 14G 11G 1.8G 86% /
/dev/loop0 104M 104M 0 100% /var/lib/snapd/snap/core/16928
/dev/loop1 8.5M 8.5M 0 100% /var/lib/snapd/snap/gping/13
tmpfs 1.9G 16K 1.9G 1% /tmp
/dev/sda2 974M 245M 663M 27% /boot
/dev/sda1 599M 20M 580M 4% /boot/efi
/dev/sda3 14G 11G 1.8G 86% /home
tmpfs 379M 132K 379M 1% /run/user/1000
tmpfs 379M 44K 379M 1% /run/user/1001
Superuser access is not required to check disk space using the df command unless you need to look into directories for which you don’t have read access. Note that, in the command below, I can ask about george’s home directory, but not about its contents.
$ df /home/george
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 13974528 11385960 1873096 86% /home
$ df /home/george/report
df: /home/george/report: Permission denied
The df command with the -T option displays the partition and file system type for the selected directory.
$ df -T /home
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda3 btrfs 13974528 11386752 1872304 86% /home
Using the du (disk usage) command
The du command reports on disk usage but, if you run this command in your home directory, it will report on every file. To view overall disk usage, run a command like this:
$ du -sk
144200 .
The number above represents the size of the current directory. The dot simply represents the current location. A command like that shown below reports the same data.
$ du -sk /home/shs
144200 /home/sh
Notice that using the -sk options, you also don’t see details on subdirectories.
$ du bin
12 bin/NOTES
24 bin
$ du -sk bin
24 bin
Superuser access is required if you want to report on directories for which you don’t have adequate permission.
# du -sk *
24 cookie
16 dumdum
431784 fedora
32 george
48 lola
16 newuser
144200 shs
Like df, du has an option for the “human-readable” format.
$ du -h bin
12K bin/NOTES
24K bin
Use the du -sh command to show the size of your current directory.
$ du -sh .
141M .
Directories for which you don’t have read access will be obvious and the sum shown will not reflect their contents. The last line in the output below lists the size of the current directory.
$ cd /home; du -sh .
du: cannot read directory './fedora': Permission denied
du: cannot read directory './newuser': Permission denied
du: cannot read directory './george': Permission denied
du: cannot read directory './lola': Permission denied
du: cannot read directory './dumdum': Permission denied
du: cannot read directory './cookie': Permission denied
141M .
A command like that below will display disk usage for the largest files or directories in the current file system location.
$ du -ah /home/shs | sort -rh | head -n 5
141M /home/shs
115M /home/shs/.cache
48M /home/shs/.cache/gnome-software
46M /home/shs/.cache/mozilla/firefox/c8n9kgaz.default-release
46M /home/shs/.cache/mozilla/firefox
Using the ls command
The ls command generally lists files by name, but it can also display files in size order with the -lhS options.
$ ls -lhS | head -5
total 708K
-rw-r--r--. 1 shs shs 3.9K Nov 8 2023 bash_builtins
-rw-r--r--. 1 shs shs 2.5K Nov 17 2023 AllTables
drwxr-xr-x. 1 shs shs 1.9K Jan 2 12:38 videos
-rwx------. 1 shs shs 1.7K Nov 27 2023 buildTable
And don’t forget that you can turn commands like this into aliases to make them easier to use.
$ alias bysize="ls -lhS"
Using the fdisk command
The fdisk command can provide useful stats on your disk partitions. Here’s an example:
$ sudo fdisk
fdisk: bad usage
Try 'fdisk --help' for more information.
$ sudo fdisk -l
Disk /dev/sda: 14.91 GiB, 16013942784 bytes, 31277232 sectors
Disk model: KINGSTON SNS4151
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: 818C0FC9-CBBA-49E5-953B-A511A4E40A3D
Device Start End Sectors Size Type
/dev/sda1 2048 1230847 1228800 600M EFI System
/dev/sda2 1230848 3327999 2097152 1G Linux filesystem
/dev/sda3 3328000 31277055 27949056 13.3G Linux filesystem
Disk /dev/zram0: 3.7 GiB, 3967811584 bytes, 968704 sectors
Units: sectors of 1 * 4096 = 4096 bytes
Sector size (logical/physical): 4096 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/loop0: 103.99 MiB, 109043712 bytes, 212976 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
Disk /dev/loop1: 8.44 MiB, 8847360 bytes, 17280 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
Using the lsblk command
The lsblk command provides very useful information on your disk partitions. The output shown below clearly shows that the disk (sda) had three partitions and their sizes and mount points.
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 104M 1 loop /var/lib/snapd/snap/core/16928
loop1 7:1 0 8.4M 1 loop /var/lib/snapd/snap/gping/13
sda 8:0 0 14.9G 0 disk
├─sda1 8:1 0 600M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 13.3G 0 part /home
/
zram0 252:0 0 3.7G 0 disk [SWAP]
Wrap-up
These commands described in this post can help you evaluate disk storage, diagnose disk usage issues and manage disk space effectively.
Source:: Network World