Monitoring Disk Space Usage in Linux

Published November 28, 2023

 Let’s monitor Disk Space Usage in Linux

This article will show you how to check how much space is left on your Linux computer using two commands: df helps you see storage space, and du keeps track of how much disk space your files are using.”

 Disk space usage in Linux

Read More ;3 Techniques to Rename a Directory in Linux

How to Analyze the Hard Drive in Linux

Let’s dive into the distinct functions of the df and du commands by discussing them separately. But before we proceed, make sure you’re connected to your remote server either through Terminal or an SSH client such as PuTTY.

How to Check Disk Space in Linux Using the df Command

The df command, or Disk Filesystem, is used to check disk space. It displays the amount of used and available storage space in your machine.

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

Disk space usage in Linux

  • Filesystem – provides the file system name.
  • Size – provides the total size of the specified file system.
  • Used – shows disk space usage in the particular file system.
  • Available – specifies the remaining disk space in the file system.
  • Use% – displays the amount of used disk space in percentage.
  • Mounted On – tells us a particular file system’s mount point.

Add an option to the df command for detailed disk space checking in Linux. Popular options include:

  • df -h – shows the result in a human-readable format.
  • df -m – displays file system usage information in MB.
  • df -k – tells users the file system usage in KB.
  • df -T – specifies the file system type in a new column.
  • df /home – allows you to view information about a specific file system in a readable format. In this example, it’s the /home file system.
  • df – help – lists other options to use and their descriptions.

How to Check Disk Usage in Linux Using the du Command

The Disk Usage command, also known as du, provides a comprehensive overview of the disk usage for files and directories on your Linux system. To utilize this powerful SSH command, you simply need to specify the folder or file that you wish to examine. Here’s the syntax you should follow:

du <options> <location of directory or file>

Consider these du command usage examples with the /Desktop directory:

  • du /home/user/Desktop – allows users to see the disk usage of their Desktop folders and files, including subdirectories.
  • du -h /home/user/Desktop – displays information in a human-readable format, similar to the -h option for the df command.
  • du -sh /home/user/Desktop – gives us the total size of a specified folder, namely Desktop.
  • du -m /home/user/Desktop – provides us with folder and file sizes in MB. Replace -m with -k to see the information in KB.
  • du -h – time /home/user/Desktop – retrieves the last modification date of the displayed folders and files.
  •  df –help – displays a list of available options with their usage.

How to Combine Commands and Clean Disk Space

Begin by using the df command to identify the file system requiring immediate cleanup. Next, combine the df and du commands with other arguments to obtain detailed information, providing insights into which files you can delete to free up disk space.

How to Sort Files By Size

Run the du command on the /Desktop directory to list files and folders in a readable format. Then, pipe the results to the sort command with the -rn option to actively sort all files and folders by size in descending order, checking the disk space usage on your Linux system.

-h /home/user/Desktop | sort -rn

It is crucial to exercise caution when deleting files, as their size alone should not be the sole determining factor. Blindly deleting files without proper consideration could result in the loss of critical data that could potentially damage your project or virtual private server. Therefore, it is imperative to take the time to carefully assess each file before making any decisions to delete them.

How to Exclude by File Size

If you want to see all files beyond a certain size, the most effective way is to use the command below:

du -h /home/user/Desktop | grep ‘^\s*[0-9\.]\+G’

The grep command lets us search files based on a specified pattern. In this example, the script will output all files bigger than 1 GB. If you want to list files over 1 MB, replace G with M.

How to Exclude File Types

If you want to exclude a particular file format from the search results, use the following combination:

du -h /home/user/Desktop/ –exclude=”*.txt”

The –exclude=”.txt” argument tells the du command to display all file formats except .txt documents.

Read More; 6 Ideal File Transfer Protocol Clients in Windows, Mac, Linux
Get Connected on meta , X, Instagram .