DEV Community

Cover image for Disk space management in Linux: 3 Easy steps
Jash Gopani
Jash Gopani

Posted on

Disk space management in Linux: 3 Easy steps

Recently I encountered a situation when my VM was running low on disk space and I had to free up some space using CLI. Below are the 3 simple steps that anyone can follow to do the same on a Linux machine.

My VM had Red Hat Enterprise Linux (RHEL) installed on it but the commands are common to all Linux distributions.

Step 1 - Check the free space on disk

Use the df command with the -h argument which makes the output human readable i.e. it shows the values in MBs and GBs instead of bytes

Command and Output

(base) [jbgopani@vclvm178-31 ~]$ df -h
Filesystem                                                 Size  Used Avail Use% Mounted on
devtmpfs                                                   4.0M     0  4.0M   0% /dev
tmpfs                                                       25G     0   25G   0% /dev/shm
tmpfs                                                      9.8G   33M  9.8G   1% /run
/dev/vda3                                                  120G   74G   46G  62% /
Enter fullscreen mode Exit fullscreen mode

Since we have not specified any path here, this command will output the free and used space on all the disks mounted to the VM

Looking at the output we can get an idea of which drives are full and need to be cleaned up. Let's say I want to free up space on /dev/vda3 disk partition which is mounted to the path /. Next step is looking at the disk usage of the files and folders in that location

Step 2 - Checking Disk Usage

Command and output

(base) [jbgopani@vclvm178-31 ~]$ du -h / --max-depth=1 2>/dev/null
337M    /boot
0       /dev
0       /proc
752K    /run
0       /sys
76G     /tmp
26M     /etc
4.0K    /root
888M    /var
7.3G    /usr
0       /afs
46G     /home
.
.
.
Enter fullscreen mode Exit fullscreen mode

The du command gives us the disk usage of each file / folder.
-h - For human readable format like before
/ - The path where we want to check disk usage (can be any path)
--max-depth=1 - This is very important, else by default this command would recursively go through all sub paths and print out information which is not something we want
2>/dev/null - This part is optional. The purpose of this is to discard any error message outputs from the stderr or error message stream. This is a standard way to discard error messages of a command in linux. The purpose of writing this here is to avoid any Permission denied errors for any files or folders which you might see if the VM is shared among multiple users

Use this command repeatedly to identify which folders are occupying the most space

Optionally, you can also sort the output of this command in decreasing order of disk usage

(base) [jbgopani@vclvm178-31 ~]$ du -h /home/jbgopani/ --max-depth=1 2>/dev/null | sort -hr
34G     /home/jbgopani/
15G     /home/jbgopani/miniconda3
5.6G    /home/jbgopani/Projects
5.6G    /home/jbgopani/pipelines
3.1G    /home/jbgopani/.vscode-server
2.1G    /home/jbgopani/.cache
924M    /home/jbgopani/Downloads
637M    /home/jbgopani/.nextflow
613M    /home/jbgopani/.etetoolkit
Enter fullscreen mode Exit fullscreen mode

Step 3 - Remove unwanted files

Now that we know which files/folders need to be cleaned up, remove them using the rm command

Example

rm -rf /home/jbgopani/Projects/tempProject
Enter fullscreen mode Exit fullscreen mode

Image description

Hope you find this quick tutorial useful. Let me know in the comments if you have any feedback/suggestions

Top comments (0)