How to check disk space in Linux (df du)

In this tutorial, we will show you how to use the df command to check disk space in Linux and the du command to monitor disk usage. If you are a Linux user, these two essential tools will help you manage your files more effectively.

Analyze hard drive
The df and du commands have a slightly different purpose when analyzing a hard drive. To avoid confusion, we will explain them in separate sections. Let's start with the df command.

Check disk space on Linux using df command
df, which stands for Disk Filesystem is used to check disk space. It will show the available and used storage of the file systems on your machine.

To run the command, follow these steps:

  • Press Control + Alt + T to open Terminal.
  • Enter the command df -h .
  • In the Available column , you will see the available space of your disk partitions. You will find your main partition at /dev/sda or /dev/sdb .

When running this command, you will see the default columns: Filesystem, Size, Available, Used, Use% and Mounted On . Generally it should look like this:


  • Filesystem: Gives you the name of the file system.
  • Size: tells you the total size of each file system.
  • Used: Shows how much space each file system is using.
  • Available: Shows how much available space is left on the file system.
  • Use%: shows the percentage of the space that is being used.
  • Mounted On: tells us the mount point of a file system.

By adding a series of options to the df command , you can check the disk space in Linux more accurately. These are the most popular options:

  • df -h : will show you the output in a human-readable format .
  • df -m : This command line is used to display file system usage information in MB .
  • df -k : to display system usage in KB .
  • df -T : This option will display the filesystem type (a new column will appear).
  • df –ht /home : allows you to view information from a specific file system in a human readable format (in this case the /home file system ).
  • df --help : will list other useful commands you can use, with their descriptions.

Check disk space in Linux using the du command
Another very useful command is du , short for Disk Usage . It will show you details about disk usage of files and directories on a Linux computer or server. With the du command, you need to specify which folder or file you want to check. The syntax is as follows:

du <options> <location of directory or file>
Let's see an actual use of the du command with the Desktop directory:

  • du /home/user/Desktop/ : This command line allows users to view the disk usage of files and folders that are in the Desktop directory (subdirectories are included as well).
  • du -h /home/user/Desktop/ : As with df , the -h option displays information in a human-readable format.
  • du -sh /home/user/Desktop/ : The -s option gives us the total size of a specified folder (in this case, the Desktop directory).
  • du -m /home/user/Desktop/ : The -m option gives us the sizes of folders and files in Megabytes (we can use -k to see the information in Kilobytes ).
  • du -h --time /home/user/Desktop/ : This reports the last modified date of the displayed files and folders.
  • df --help : Shows a list of available options and what they can be used for.

Combine commands to clean disk space
You can get more information by combining the df and du commands with other arguments. By doing this, you will have a better idea of ​​what files you can delete to free up disk space.
Just remember to start with the df command to see which file system needs the most cleanup. After that, you can proceed with these combinations.

Sort files by size
First, we assemble the files and folders on Desktop into a readable format using the du command . We then use pipe to send the result to the sort command, along with the -rn option . The script will sort all files and folders from largest to smallest to check disk space usage on Linux. The combination should be like this:

du -h /home/user/Desktop/ | sort –rn
Remember that you should not necessarily delete files just because they are large. If you're not careful, you could delete essential files that would break your project.

Exclude by file size
Suppose you want to see all files that are larger than a certain size. The most effective way to do this is by using the command shown below:
du -h /home/user/Desktop | grep '^\s*[0-9\.]\+G'
The grep command allows us to search for files based on a specified pattern. In this example, the script will return all files larger than 1 GB. If you want to select the data larger than 1 MB, you can replace G with M .

Exclude file types
The last combination is useful when you need to exclude a particular file format from the search results. For example:
du -h /home/user/Desktop/ --exclude="*.txt"
The -exclude=».txt» argument causes the du command to display all file formats except .txt documents.

Conclusion
The df and du commands are file management tools that check disk space in Linux and display all files stored on your machine. It allows you to add certain options (like -h, -m, -k, etc.) to refine the output according to your needs.

What's cool is that users can get a more specific result by combining du and df with other commands, like sort, grep and exclude . Together, they will help you better understand how disk space is being used on your server.

No comments

Powered by Blogger.