We use files and directories in Linux as windows. We can create them, rename them or delete them according to our need. In this lesson, we will learn remove directory Linux commands. In other words, we will focus on how to remove a directory in Kali Linux Operating System. In another lesson, we will focus on deleting a file in Linux. You can also check it.
By the way, we should remember that, rmdir and rm commands deletes these directories permanently. If you delete a wrong directory, you can not restore this directory easily. So, you should be careful while using rmdir and rm commands of Linux. But, there is still ways to restore these directories if you are an experienced Linux administrator. Because, after deleting any file, it is not deleted from the hard drive.
Basically, there are two commands that we use as remove directory Linux command. The directory remove commands are given below:
Directories can be empty or it can have sub directories or files under it. Here, according to this situation, we can use different commands.
If we want to remove an empty directory, we use, Linux rmdir command.
If we want to remove a directory which has sub directories or files under it, then we use Linux rm command. We can also use rm command to remove empty directories. To do this, we use rm- d option.
You can also DOWNLOAD Linux Cheat Sheet!
Table of Contents
There are different options used with Linux rm command while we are deleting existing directories. These options are given below:
Now, let ‘s give examples to these rm options. Firtly, we will create a directory named abc with Linux mkdir command. This is an empty directory.
Now, let’s try to delete this directory with rm command. It will not delete the directory and it will give an error. Because, this is an empty directory. It can be deleted with Linux rmdir command or rm command with –d option.
Now, let’s create our abc directory again and then remove it with rm command.
This time, we will create a directory with files under it. Our directory will not be an empty this time.
We can see the files of this directory hierarchically with tree command.
Now, let’s try to delete this directory and the files under it with both rm and rmdir commands. As you can see below, both will be unsuccessful because we can not delete directories with files with these commands. We can do this with only rm command with option -r.
Now, let’s try with rm -r command.
As you can see, the directory has deleted.
Now, we can create a directory with 3 files in it and then, we will delete it with also -v option to see the steps.
We can also use rm -i option to be asked “Are You Sure?”. This time, it will ask us each steps, for each file and the complete directory.
We can also sue different options with Linux rmdir command. These options are given below:
Let’s give examples to learn rmdir command better. Think about that we have one more files beginning with the same keyword and we would like to delete all of them. Then we can use the common part following with an asterisk (*) to remove all of them with one command.
As you can see here, both abc and abc1 directory has deleted.
If there is another directory in a directory, we can delete them in two ways. One of them is deleting them one by one by writing both directory ‘s path like below:
We can do this with rmdir -p option without writing each path. Here we will use, rmdir -p option with subdirectory name and it will delete also its hierarchically parent. Here, important point is using the full path of this subdirectory.
In this remove directory linux lesson, we have learned two important commands of Linux: rmdir and rm. You will use these commands too much in your Linuc operations. But be careful!