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
The -h
option can be used along with df command to show the results in a human-readable format.
# df -h
The below command can be executed to list the disk space with file system types.
# df -Th
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 /
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
Using the -s
switch summarizes and shows the total disk usage by the argument value passed along with the du
command.
# du -s /boot
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
The total disk space usage in human-readable format can be displayed by using the -h
switch in the above command.
# du -sch /boot
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
With Debian and Ubuntu distributions, the APT’s version
of the autoremove command can be used.
# apt autoremove
For other distributions using dnf
, the below command can be used.
# dnf autoremove
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
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
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
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
Remove the package that is no longer needed by using the below command:
# sudo apt remove <package name=""> -y
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
Run the below command to remove the package that is no longer needed.
# yum remove <package name=""> -y
OR
# dnf remove <package name=""> -y
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