Linux Commands for Checking How Much Space Is Left on Disk

duroemmanuel

Kolawole D Emmanuel

Posted on January 4, 2024

Linux Commands for Checking How Much Space Is Left on Disk

Introduction

Managing disk space is important to maintaining a healthy and well-functioning Linux system. Files and programs accumulate over time taking up valuable disk space that can potentially slow our system. However, to address potential issues such as system errors, slowdowns, data loss, and other performance-related concerns, it's crucial to have a clear understanding of the remaining space on our disk drives.
In this tutorial, we'll explore two essential Linux commands as well as practical examples for checking disk space.

Using df Command

The df  (disk free) command is a fundamental tool for displaying information about the total space, used space, available space, and filesystem type for all mounted partitions.

Basic Usage

Let's check the disk space using the df command. To start with, we'll run the command without an option because the output can be complex. Furthermore, this will display the available and used space for all mounted filesystems on the Linux system:

df 
Enter fullscreen mode Exit fullscreen mode

The results displayed consist of six columns:

  1. Filesystem: displays the name of the filesystem
  2. 1K-blocks: represents the total size of the filesystem or storage device in 1-kilobyte blocks
  3. Used: shows the amount of disk space used by files and data on the filesystem
  4. Available: indicates the number of 1K-blocks that are available or unused on the filesystem
  5. Use%: displays the percentage of disk space used on the filesystem
  6. Mounted on: specifies the mount point of the filesystem

Now that we have a foundational understanding of the df command. let's delve into various options that enhance its functionality and display a more understandable result.

-b Option

The -b (Block size) option displays a more useful output and information by replacing the 1K-block counts. To utilize this option, input the df -b command followed by a letter from the list, which includes K (kilobyte), M (megabyte), G (gigabyte), T (terabyte), P (petabyte), E (exabyte), and Z (zetabyte). For example, let's check the disk space left in kilobytes:

df -BK
Enter fullscreen mode Exit fullscreen mode

In the result, the command df -bk option displayed the Available space in kilobytes.

Also, we can check the disk space left in Megabyte:

df -BM
Enter fullscreen mode Exit fullscreen mode

The command df -bm option displayed the Available space in megabytes. However, add the letter to the -b option to check for the rest of the list.

-h Option

Use the option to present the output in a human-readable format, which could include sizes in Gigabytes, Megabytes, and even Kilobytes, making it easy to understand.

Let's check the disk space usage of only the /home file using the -h option:

df -h /home
Enter fullscreen mode Exit fullscreen mode

The df -h command displays the system filename, total size, used space, available space, and, mount point. In the result, we can see that the space left is 37GB.

-i Option

Use the -i option to display inode-related details. Inodes are data structures that store information about files on a filesystem. Let's use the -i option:

df -i
Enter fullscreen mode Exit fullscreen mode

When using the df -i command, the resulting output provides a comprehensive view of both disk space and inode usage, offering a more holistic understanding of the filesystem. Notably, the IFree column indicates the remaining disk space for each of the filesystems.

Using du Command

The du (disk usage) command is a tool that provides a granular analysis of disk usage within specific directories. Also, it's used to discover what's taking up the used disk space.

Basic Usage

Running the du command without an option will show a list of all directories and subdirectories with the sizes. Firstly, let's check the directories holding data using the du command without an option:

du
Enter fullscreen mode Exit fullscreen mode

Within the output, each line presents a pair of columns that indicate the size and name of a directory. However, by default, the size is in 1K-Block.

Various options can be used with the du command to display a desired result based on the need. Additionally, let's explore various options that enhance its functionality.

-h Option

As explained earlier, the option displays the result in human-readable format. Secondly, let's check how much space our /Desktop directory is using:

du -h
Enter fullscreen mode Exit fullscreen mode

The du -h command shows the list of subdirectories of the Desktop directory and how much space each of the subdirectories is using. Also, the ./.git directory is holding the most used space of 568K.

We can further optimize the -h option by using it with the -s (summarize) command to display the total space used without showing the subdirectories. Additionally, let's take a look at a command that provides information in a summarized format for all directories in the current work directory:

du -s -h *
Enter fullscreen mode Exit fullscreen mode

In this result, the (*) specifies all directories in the current directory we're running the command from (working directory).

-c Option

The -c option allows us to display a total of the disk space used by the specified directories, including their subdirectories. Thirdly, let's explore this option using the Document directory as a case study:

du -c -h
Enter fullscreen mode Exit fullscreen mode

When executing the du -c -h command, the output showcases the disk space usage of each subdirectory within the Document directory. Additionally, it presents a cumulative total at the conclusion, providing a comprehensive overview of the space utilization. However, we can see that the total space is 22M

-d Option

The -d option allows us to set the depth at which the du command will display information. For example, we can limit the output to a specific depth level. Lastly, let's analyze the Document directory at a depth level of 2:

du -h -d 2
Enter fullscreen mode Exit fullscreen mode

By utilizing the du -h -d 2 command, we can obtain valuable insights into the Document directory and its immediate subdirectories. This approach offers a more targeted perspective on the space usage, enabling us to focus our attention on specific areas.

Conclusion

The df and du are indispensable tools for effectively checking how much space is left on the Linux system. while the df command provides a comprehensive overview of filesystem usage with versatile options for tailored analysis, the du command takes a different approach by focusing on granular-level analysis, specifically highlighting space-consuming directories. Finally, mastering these commands equips administrators and users with the essential skills to maintain optimal system performance and prevent storage-related issues.

💖 💪 🙅 🚩
duroemmanuel
Kolawole D Emmanuel

Posted on January 4, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related