Check Disk Space in Linux Using df and du Commands

Check Disk Space in Linux Using df and du Commands

It doesn’t matter if you are a system administrator or a regular desktop user, managing disk space in Linux is an important task. However, Linux has a strong built-in commands to check disk space called ‘df‘ adn ‘du’.

The df command stands for disk filesystem. It is used to get a full summary of available and used disk space usage of the file system on Linux system.

The du command, short for disk usage, is used to estimate file space usage. The du command can be used to track the files and directories which are consuming excessive amount of space on hard disk drive.

Check Disk Space in Linux Using df Command

The easiest and more popular way to check your disk space is to run the df command.

$ df <options>

If you run the df command, this is the output that you would get.

ubuntu:~$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev             2007152        0   2007152   0% /dev
tmpfs             403960      716    403244   1% /run
/dev/vda1       81120644 31236604  49867656  39% /
tmpfs            2019792        0   2019792   0% /dev/shm
tmpfs               5120        0      5120   0% /run/lock
tmpfs            2019792        0   2019792   0% /sys/fs/cgroup
/dev/vda15        106858     3668    103190   4% /boot/efi
tmpfs             403956        0    403956   0% /run/user/0
tmpfs             403956        0    403956   0% /run/user/1000

By default, the df command shows six columns :

  • Filesystem: the name of the filesystem that may be equal to the partition name on your filesystem (/dev/vda1 or /dev/sda1 for example);
  • 1K-blocks: the number of blocks on the filesystem of size 1Kb. The /dev/vda1 contains 81120644 1K-blocks, it has 81120644000 bytes or 78,5 gigabytes of space in total;
  • Used: the number of 1K-blocks used on the filesystem;
  • Available: the number of 1K-blocks available for the filesystem;
  • Use %: the percentage of disk used on the filesystem;
  • Mounted on: the mountpoint used in order to mount the filesystem.

Show Disk Space Usage in Human Readable Format

Have you noticed that above commands displays information in bytes, which is not readable at all. We are in a habit of reading the sizes in megabytes, gigabytes etc. as it makes very easy to understand and remember.

The df command provides an option to display sizes in Human Readable formats by using -h (prints the results in human readable format (e.g., 1K 2M 3G)).

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           395M  716K  394M   1% /run
/dev/vda1        78G   30G   48G  39% /
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
tmpfs           395M     0  395M   0% /run/user/1000

Check Specific Filesystem Disk Space

In some cases, you may want to restrict your search to specific filesystems on your host. To check disk space for a specific filesystem, you have to run the following command:

$ df <options> <path>

For example, to check disk information related to the root mountpoint, you would run:

$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        78G   30G   48G  39% /

Look at Specific Fields on the Output

To achieve this result, simply append a -output flag on your commands.

$ df -h --output='field1','field2' /

For example, to show only the size and the usage percentage, you would run:

$ df -h --output='size','pcent' /
Size Use%
78G  39%

Check inode Usage on Linux

In some very specific cases, you may need to check the inode usage on your Linux filesystems.

The files are tightly coupled with inodes on the filesystem. However, this inode table is limited in size. As a consequence, you may run out of inode entries before running out of disk space.

If your system already handles too many files (tiny or not), it won’t be able to allocate more space to new files, even if your disk has 30GBs available.

In order to check inode usage on Linux, append the -inodes flag to the df command.

$ df --inodes
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
udev             501788    384   501404    1% /dev
tmpfs            504948    617   504331    1% /run
/dev/vda1      10321920 320672 10001248    4% /
tmpfs            504948      1   504947    1% /dev/shm
tmpfs            504948      4   504944    1% /run/lock
tmpfs            504948     18   504930    1% /sys/fs/cgroup
/dev/vda15            0      0        0     - /boot/efi
tmpfs            504948     19   504929    1% /run/user/0
tmpfs            504948     10   504938    1% /run/user/1000

Similarly, you can check inode usage in a human readable way by specifying the “-h” option.

$ df -h --inodes /
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/vda1        9.9M  314K  9.6M    4% /

Check Disk Space in Linux Using du Command

The du command is used in order to have disk usage information related to directories and files on your system.

$ du <option> <path|file>
$ du <option> <path1> <path2> <path3>

As specified, you can either have the disk usage for a specific file, or for a path on the system.

Check File Disk Usage

In order to check the disk usage of a specific file, simply append the name of the file to the du command.

For example, to check the size of the .bashrc file, you would run:

$ du ~/.bashrc
4       /home/user3/.bashrc

By default, sizes are displayed in kilobytes. However you can change the default display by specifying a size format with the -B option.

$ du -BM ~/.bashrc
1M      /home/user3/.bashrc

Note that sizes will be rounded to the nearest whole number. In this case, the .bashrc file is not 1Mb large in size, but it has been rounded automatically.

Of course, you can also choose to have sizes displayed in a human readable format, verifying that sizes are indeed displayed in kilobytes.

$ du -h ~/.bashrc
4.0K    /home/user3/.bashrc

The “-h” option can be combined with the -apparent-size option in order to get the real size of a file without rounding.

$ du -h --apparent-size ~/.bashrc
3.7K    /home/user3/.bashrc

Check Directory Disk Usage

On the other hand, you can check directory disk usage by specifying a path to the du command.

For example, if you are looking for the disk usage for all directories on the filesystem, you would run:

$ du /
...
0	/run/udev/links/\x2fdisk\x2fby-id\x2fwwn-0x5002538e09a787b4-part3
0	/run/udev/links/\x2fdisk\x2fby-partuuid\x2f6bd9a838-03
0	/run/udev/links/\x2fdisk\x2fby-id\x2fwwn-0x5002538e09a787b4
0	/run/udev/links/\x2fdisk\x2fby-path\x2fpci-0000:00:1f.2-ata-1
0	/run/udev/links
868	/run/udev/data
868	/run/udev
4	/run/tmpfiles.d
0	/run/initramfs
1384	/run
448393904

However, in some cases, you don’t want to see the output for every single file or directory on the path, but rather a total for the given path.

To check the total disk usage for a given path, use du with the -shc option.

$ sudo du -shc /home
2.8G    /home
2.8G    total

Here is a breakdown of the options used :

  • -s : for summarize, it will display a total for each path provided instead of the full listing of every single file and directory in it;
  • -c : for grand total, if you specify multiple paths, it will sum them in order to produce a grand total for them;
  • -h : for human readable, it will display results with units.

Conclusion

As you can see the df and du commands are a very useful to check disk space in Linux. By refining the information returned by df and du it is easy to find out how much hard disk space is in use, and to discover what is taking up that space. Therefore, you can make an informed decision about moving some data to other storage, adding another hard drive to your computer or deleting redundant data.

These commands have a lot of options. We described only the most useful options here. You can see a complete listing of the options for the df command and for the du command in the Linux man pages.

Leave a Reply

Your email address will not be published.