VPS

How to Check Disk Space Usage in Linux

Introduction

In this tutorial, you will learn you how to check disk space on Linux machine using command line. You will learn certain separate commands which combined can become a powerful tool in disk management. This tutorial might help you in case you need to free up some disk space on your machine or if you simply want to see which files are taking up most of your hard drive.

What you’ll need

Before you begin this guide you’ll need the following:

  • Access to the command line

Step 1 — Analyzing hard drive

The first command that you should learn is df. This command stands for disk filesystem. It will display available and used disk space of file systems on Linux machine. When executing this command, you will see certain default columns like Filesystem, Size, Used, Available, Use%, Mounted On. It should look something like this:

df output

  • FileSystem – Provides us the name of the filesystem.
  • Size – Gives us a total size of the particular filesystem.
  • Used – Shows how much disk space is already in use in the particular file system.
  • Available – Shows how much space is left in the particular file system.
  • Use% – This shows how much disk space in percentage has been already used.
  • Mounted On – The last column tells us the mount point of that file system.

More columns can be shown by using certain options with this command.

We recommend using this command with additional options like these:

  • df -h – It will display in a better, more readable human format. Using this command, disk space will be shown in GB (unless it’s less than one GB, then it will be displayed in MB or even B).
  • df -m – Can be used to display information of file system usage in MB.
  • df -k – Same as the previous one, can be used to display information of file system usage in KB.
  • df -T – This option will show the file system type (a new column will appear).
  • df -ht /home – Using this option, you can view information about a specific file system (in a human readable format).
  • df --help – Will show all the other useful commands that you can use and provide more information about them.

Another useful command is du. It stands for Disk Usage. This command will show you information about disk usage of files and directories on a Linux machine. This command is very useful since it provides a possibility of recursively gathering information about file and folder disk usage. You can use this command to look into a specific folder and see information only regarding what’s inside of it. Here are some of the more useful options to use with this command:

  • du /home/user/Desktop/ – This option allows to see into user’s Desktop and it will recursively provide information regarding disk usage of folders and files which are on our Desktop (which means that all the subdirectories are included as well).
  • du -h /home/user/Desktop/ – Just like with df, the option -h displays information in a more Human readable format.
  • du -sh /home/user/Desktop/ – Option -s will give us the total size of a specified folder (in this case, it will show the total size of folder Desktop).
  • du -m /home/user/Desktop/ – Just like with df, this will provide us information in Megabytes (we can use -k to see information in Kilobytes.
  • du -h --time /home/user/Desktop/ – This will give us information based on last modification date of the files and folders displayed.
  • df --help – Will show all the other useful commands that you can use and provide more information about them.

Step 2 — Combining commands and cleaning disk space

Using the commands we have learned in this tutorial and other useful commands which you can learn more about by reading our other tutorials, we can combine them and get some useful results which may help us determine what needs to be deleted to free up some disk space! So here are some tricks and tips that we would like to share with you:

  1. Before doing anything further, always remember to first check which disk partition is “injured” the most. You can do that with an already learned command df.
  2. The most popular combination is probably this one: du -h /home/user/Desktop/ | sort -rn. First, we gather the list of files and folders from our Desktop in a Human readable format and we fetch this result to another command called sort, which, using the right options, sorts our list of files and folders starting from the biggest to the smallest and this gives us a better and more comfortable idea on where we should focus on if we need to free up some disk space.
  3. Another very useful combination would be: du -h /home/user/Desktop | grep '^\s*[0-9\.]\+G' It’s useful in case you have a lot of small files and want to filter out the big ones. You can also use +M instead of +G as the latter filters files which take at least a Gigabyte of space and “M+” will give you a list of files which are heavier than one MB.
  4. You can exclude the certain type of files with an option --exclude="*.bin". It is used with command du in case you want to fetch a list of files and you do not want that list to contain certain type of files. For example:
    du -h /home/user/Desktop/ --exclude="*.txt" | sort -rn – This would exclude .txt type of files from the resulting list.

Conclusion

In this tutorial, you have learned the basic commands which will help you when it comes to checking and managing disk space of your Linux machine. For more information, use man command, this will provide you a manual with even more information about the command and options that you can use with it. Also, do not forget to check out our other bash and command line related tutorials!

About the author

Tautvydas V.

Tautvydas started his career as a technical support agent and now walks the path of full-stack development. He strives to produce top-notch features, improvements, and outstanding user experience with every line of code. In his free time, Tautvydas likes to travel and play old school video games.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More in VPS
How to Install Let’s Encrypt SSL on CentOS 7 Running Apache Web Server

Close