Delete directory linux

Table of Contents

Introduction

Are you wondering if it is possible to delete a directory using the linux command line directly? You are in the right place!

This tutorial will show you how to delete a directory using the rm command.
Since this command has several options, we will also see the most common and useful ones.

For this tutorial it might be useful to know how to create a new directory from the linux terminal.
If you don’t know how to do it or are simply interested in learning more, I suggest you take a look at this post.

How to delete a directory – rm linux

To delete an existing directory or file in linux you can use the rm command.

Let’s see now the structure of rm command:

rm [OPTIONS] [DIRECTORY]

In the next sections we will differentiate between deleting an empty directory and one that contains some files.
In fact, as we will see, it is possible to always use the same rm command but the parameters to be used will be different.

How to delete an empty directory – rm -d linux

Let’s start from the simplest case, that is deleting an empty directory.
For this specific case, we need to use the -d/--dir parameter.

Suppose we want to delete a directory named my_new_dir located in the working directory.
The command to use will be the following:

rm -d my_new_dir

As you can imagine, if the folder we want to delete is in another directory, for example /tmp, we will have to specify the path.
However, the usage of the parameters remains the same.

rm -d /tmp/my_new_dir

The only thing you must not forget is that the -d parameter only works if you are deleting an empty directory.
In the next chapter we will see how to delete a directory that contains other files and/or directories.

How to delete a not-empty directory – rm -r linux

If, as in this case, you are interested in deleting a not-empty directory you will need to use the rm command with the -r/-R/--recursive parameter.
The -r parameter allows you to delete a directory recursively.

If you don’t have clear the concept of recursion, take a look at this page.

The simplest example we can do is to delete a directory in the same working directory.
Suppose we want to delete a directory called my_new_dir.

To do this, just open a linux command line and type the following command:

rm -r my_new_dir

Using the ls command, you can verify that the directory is not present anymore.
If you don’t know how to best use the ls command, I suggest you take a look at this post!

Another example we can do is to delete a directory but in a completely different path.

rm -r /tmp/my_new_dir

In this case the my_new_dir has been deleted in the tmp folder.
As you can see linux allows you to delete a directory anywhere you want, just specify the full path.

Conclusion

If you have come this far, it means that you have now learned how to delete a directory in Linux using the rm command.

As always, I recommend that you continue with the next article to learn everything there is to know about the Linux world.
Feel free to leave a comment and ask me for help if you are in trouble.

Leave a Comment

Your email address will not be published. Required fields are marked *