Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Disk Space Commands in Linux
3.
The df Command in Linux
3.1.
Usage of the df Command
3.1.1.
To check the disk space, you can use the df command in the terminal as given below:
3.1.2.
To check the disk space in a human-readable format (megabytes and gigabytes), you can use the option -h. Here is the following command to use in the terminal:
3.1.3.
To check the disk space of the specific file system, the below command can be used:
3.1.4.
To check the disk space of the file systems by their type, use can use the following command:
3.2.
Options in the df Command
4.
The du Command in Linux
4.1.
Usage of the du Command
4.1.1.
To display the disk usage of the current directory, the below command can be used:
4.1.2.
To display the disk usage with a more human-readable format (megabytes and gigabytes), you can use the -h option as given below:
4.2.
Options in the du Command
5.
Frequently Asked Questions
5.1.
What are the disk space commands in Linux?
5.2.
What are the types of disk space commands in Linux?
5.3.
How to display the disk space used in a human-readable format?
5.4.
How to check the disk space used by a specific directory?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Disk Space Command in Linux Cheat Sheet

Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

If you want to check the free disk space on your Linux machine, you can use disk space commands. Disk Space Commands in Linux are the commands which are used to check the free space in the disk and the directory size.

Disk Space Command in Linux Cheat Sheet

In the article “Disk Space Command in Linux Cheat Sheet”, we will discuss what these commands are, and how many types there are with the implementation. We will also discuss the options for the command that you can use for a better, finer-grained view of disk usage.

Read about Linux - Commands List and File Management in Linux.

Disk Space Commands in Linux

Checking the space of the disk in Linux is one of the most important tasks. Let’s say there is a package that is supposed to be downloaded on your computer but gets failed because of the space. So it's very important to check and manage the free disk space for the better optimal performance. Disk Space Commands help you to show how the disk space is used.

In Disk Space Commands in Linux, there are mainly two commands, “df” and “du,” which are used to check the disk space in Linux and to display the disk usage by the individual directories in Linux. We will discuss both “df” and “du” in this article in the next sections.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

The df Command in Linux

In the article “ Disk Space Command in Linux Cheat Sheet”, now we will discuss what is “df” command. The command “df” stands for disk free, which is used to check the disk space in Linux. There are various columns that “df” commands show, which are as follows:

Filesystem Size Used Avail Use% Mounted on
  • Filesystem: The filesystem column shows the name of the particular drive that basically consists of all the hard, logical, or virtual drives.
     
  • Size: The size specifies the total size of the particular filesystem.
     
  • Used: This column shows the used space in that particular file system.
     
  • Avail: This column shows the available space in the file system.
     
  • Use%: This column shows the percentage of the disk used.
     
  • Mounted on: This column specifies the directory where the file system is located.
     

Now we will see how to use the “df” command:

Usage of the df Command

To check the disk space, you can use the df command in the terminal as given below:

df


This will simply display all the drives with this simple command in the kilobyte blocks, and the similar output you will get as below:

df output

To check the disk space in a human-readable format (megabytes and gigabytes), you can use the option -h. Here is the following command to use in the terminal:

df -h


Output:

df -h output

To check the disk space of the specific file system, the below command can be used:

df -h dev/sda1


This command will show the disk space of the particular file system “dev/sda1”, and -h is used to read it in a human-readable format.

Output:

Filesystem      Size    Used    Avail   Use%  Mounted on
/dev/sda1       100G     50G     50G    50%      /

 

To check the disk space of the file systems by their type, use can use the following command:

df -ht ext4


This command will show the disk space of the file systems with the type “ext4”, and -h is used to read it in a human-readable format.

Output:

Filesystem      Size  Used   Avail   Use%   Mounted on
/dev/sda1       100G   50G     50G    50%    /
/dev/sdb1       200G   80G    120G    40%    /mnt/storage

Options in the df Command

There are various options available which can be used with the “df” command in Linux. Here are the commonly used options as follows:

  • “-h”: This option is used to display the sizes in the human-readable format.
     
  • “-i”: This option is used to display the inode information.
     
  • “-a”: The “-a” option is used to display all the file systems.
     
  • “-x”: It is used to basically remove the file system types from the output.
     
  • “-T”: This option is used to display the file system type.

The du Command in Linux

In the article “ Disk Space Command in Linux Cheat Sheet”, now we will discuss what is “du” command. The command in Linux “du” stands for Disk Usage, which is used to display the disk used by the individual directories in Linux. The “du” command shows the finer-grained view of disk usage and shows the disk used in your current directory.

Usage of the du Command

To display the disk usage of the current directory, the below command can be used:

du

 

This command will display the sizes of all the files or directories in the current directory.

Output:

4       ./dir1
8       ./dir2
12      ./file1.txt
24      ./file2.txt

 

To display the disk usage with a more human-readable format (megabytes and gigabytes), you can use the -h option as given below:

du -h

 

This command will display the sizes in megabytes or gigabytes of all the files or directories in the current directory with the human-readable format.
 

Output:

4.0K    ./dir1
8.0K    ./dir2
12K     ./file1.txt
24K     ./file2.txt


Options in the du Command

There are various options available that can be used with the “du” command in Linux. Here are the commonly used options as follows:

  • “-h”: This option is used to display the sizes in the human-readable format.
     
  • “-a”: The “-a” option is used to display all the file systems.
     
  • “-x”: It is used to basically remove the file system types from the output.
     
  • “-s”: The “-s” command is used to display the total sum of disk usage for the current directory.
     

Read about Linux - Commands List and File Management in Linux.

Frequently Asked Questions

What are the disk space commands in Linux?

Disk Space Commands in Linux are the commands which are used to check the free space in the disk and the directory size. These commands help you to show how the disk space is used.

What are the types of disk space commands in Linux?

There are mainly two commands in Disk Space Commands, “df” and “du,” that are used to check the disk space in Linux and to display the disk usage by the individual directories in Linux.

How to display the disk space used in a human-readable format?

To check the disk space in a human-readable format (megabytes and gigabytes), you can use the option “-h”. Here is the following command to use it in the terminal: df -h.

How to check the disk space used by a specific directory?

To check the disk space of the specific file system, the below command can be used: df dev/sda2. This command will show the disk space of the particular file system “dev/sda2”.

Conclusion

Checking the space of the disk in Linux is one of the most important tasks for doing the majority of operations, such as creating a file, downloading a file (or package), and deleting a file (or package). In the article “Disk Space Command in Linux Cheat Sheet”, we discuss what these commands are, what are types of it, and how to implement them in your Linux terminal. We also discussed some of the commonly used options with both commands “df” and “du”.
 

Here are more articles that are recommended to read:

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMS, and System Design, etc. as well as some Contests, Test SeriesInterview Bundles, and some Interview Experiences curated by top Industry Experts.

Happy Learning!

Previous article
Best Backup Tools for Linux
Next article
How to Echo a New Line in Linux
Live masterclass