How to remove a directory on Linux CLI

This is a quick reference guide on how to remove a directory on Linux Based Operating Systems.

To remove an empty directory.

root@VPS:~/vps/test# ls
test1
root@VPS:~/vps/test# rmdir test1/
root@VPS:~/vps/test# ls
root@VPS:~/vps/test#

To remove multiple empty sub-directories using -p option.

root@VPS:~/vps/test# cd vps/test/test1
root@VPS:~/vps/test/vps/test/test1# cd
root@VPS:~# rmdir -p vps/test/vps/test/test1/

To remove a directory containing files or directories using -R option.

root@VPS:~# mkdir -p vps/test/test1
root@VPS:~# ls
vps
root@VPS:~# rm -R vps
root@VPS:~# ls

To remove multiple empty directories using a wildcard.

root@VPS:~/vps#
root@VPS:~/vps# mkdir vps1 vps2 vps3 abc
root@VPS:~/vps# ls
abc  vps1  vps2  vps3
root@VPS:~/vps# rmdir *vps*
root@VPS:~/vps# ls
abc  

In the above example use the *vps* to remove any directory that contains the word vps.

To remove multiple directories using a wildcard and rm -R option.

root@VPS:~/vps# ls
abc  file1.text  vps1  vps2  vps3
root@VPS:~/vps# rmdir *vps*
rmdir: failed to remove 'vps1': Directory not empty
rmdir: failed to remove 'vps2': Directory not empty
rmdir: failed to remove 'vps3': Directory not empty
root@VPS:~/vps# rm -R *vps*
root@VPS:~/vps# ls
abc  file1.text

If you are new to the world of Linux, an avid Linux enthusiast or a student why not try our 0.99p per month Linux VPS.

Simply click on the screen shot below to find out more or navigate to https://piggybank.cloud

Thank you for reading and please feel free to leave any feedback.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s