How to check and clean Linux System’s Disk Space

2021-08-26 By Aaron 57730 Views linux disk
11 reviews

On a Linux system, freeing up disk space may be required if there arises a need to install more software. Another cause could be that the system's disk space is severely low, as indicated by any relevant warning. Clearing disk space in a Linux system to free up storage space is a certainty that will arise at some point.

This guide outlines the basic steps that can be performed to check the disk space status in a Linux server and also the actions that can be carried out to free up disk space in the Linux system.


Checking disk space status in Linux

There are two commands available for finding available and used disk space in a Linux server, which are namely the df and du commands.


1. Using the df command

The df command can be used to view the available disk space for each drive on the Linux system.

By default, the df output shows the usage in KB. You can use the -h option to make the output easier to read. This option shows the amount of disk space available in kilobytes (K), megabytes (M), and gigabytes (G).


Executing the df command alone will list the disk space usage without any formatting.

# df

linuxdisk1


The -h option can be used along with df command to show the results in a human-readable format.

# df -h

linuxdisk2


The below command can be executed to list the disk space with file system types.

# df -Th

linuxdisk3


The df command can also be used to target a specific drive, using either its Filesystem or Mounted on description from the disk space results being displayed.

# df -h /dev/vda1

OR

# df -h /

linuxdisk4

linuxdisk5


2. Using the du command

In a Linux system, the du command is used to determine how much disk space is being utilized by files and folders. For this purpose, different switches can be used to retrieve the appropriate output.

The file/folder size is usually displayed in KB while using the du command. The -h switch can be used to display values in human-readable units such as KB, MB, GB and TB. To get the overall directory utilization, the -s and -c switches are used, in which the -c switch is used to get subtotal of the items.


The below set of example commands shows the different uses of the above-specified switches when used along with the du command. The example folder being considered in this context is /boot .


Using the du command alone will show the disk space usage of the folder as well as the files under it.

# du /boot

linuxdisk6


Using the -s switch summarizes and shows the total disk usage by the argument value passed along with the du command.

# du -s /boot

linuxdisk7


By using the -c switch along with the above command, the grand total disk usage value of the folder can be displayed.

# du -sc /boot

linuxdisk8


The total disk space usage in human-readable format can be displayed by using the -h switch in the above command.

# du -sch /boot

linuxdisk9


Cleaning/clearing disk space on Linux system

Once the disk space in the server is noticed to be running low, it is a requirement that enough disk space is freed up in the server for the services to be running without interruption. Some of the common actions that can be performed in order to free-up disk space in a Linux system are mentioned below:


Autoremove feature

Most package managers come with an autoremove option that can be used to remove any unwanted packages on the server. The autoremove option is used to clean up the unwanted packages and their dependencies along with it.

The autoremove option can be used to clean up unwanted packages as described below depending on the OS distribution installed in the server.


For CentOS, AlmaLinux and Rocky Linux distros, the autoremove option can be used along with yum command.

# yum autoremove

linuxdisk10


With Debian and Ubuntu distributions, the APT’s version of the autoremove command can be used.

# apt autoremove

linuxdisk11


For other distributions using dnf, the below command can be used.

# dnf autoremove

linuxdisk12


Clearing the package cache

A clean command is usually included in Linux package managers which can be used to clear the package manager's cache. It is also a good command to use if package-related issues are being generated because of corrupted metadata.


Use the below command for Debian and Ubuntu.

# sudo apt clean

linuxdisk13


There is also an autoclean option in APT. This command clears the cache for packages that are no longer available in APT's repositories.

# sudo apt autoclean

linuxdisk14


For CentOS/Fedora/AlmaLinux/Rocky Linux that uses YUM and DNF, the option related to what is to be cleared from the cache is to be indicated. Metadata, packages, and all are the most useful alternatives.


The below example YUM command deletes all cached data.

# yum clean all

linuxdisk15


Removing unused installed packages

If the system is still running on low disk space, any installed packages that are no longer used can also be removed to free up disk space.


For Debian/Ubuntu distros:

Execute either of the below commands to see the list of installed packages:

# sudo apt list --installed
OR
dpkg -l

linuxdisk16

linuxdisk17


Remove the package that is no longer needed by using the below command:

# sudo apt remove <package name=""> -y

linuxdisk18

NOTE: Replace "package name" with the actual package name that needs to be removed. In the above example, the package that is being removed is wget.


For CentOS/Fedora/AlmaLinux/Rocky Linux distros that uses YUM and DNF:


Execute any of the below commands to see the list of installed packages:

# yum list installed
OR
# rpm -qa

linuxdisk19

linuxdisk20


Run the below command to remove the package that is no longer needed.

# yum remove <package name=""> -y
OR
# dnf remove <package name=""> -y

linuxdisk21

linuxdisk22

NOTE: Replace "package name" with the actual package name that needs to be removed. In the above example, the package that is being removed is wget.


In the case of APT, the purge option can also be used to destroy all associated files with the deleting package. The rpm or dpkg commands can also be used to delete packages without affecting their dependencies.


Related Tutorials

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.