Remove Directory Linux Commands

remove-directory-linux-commands-ipcisco

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:

  • rm
  • rmdir

 

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!


 

rm command options

There are different options used with Linux rm command while we are deleting existing directories. These options are given below:

  • -d: Removes empty directories.
  • -f: Forces the removal of all files or directories.
  • -i: Asks before every removal
  • -r: Removes directories and their content recursively.
  • -v: Provides a verbose list step by step.

remove-directory-linux-ipcisco.com

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.

 

┌──(root㉿kali)-[/home/kali]

└─# mkdir abc

┌──(root㉿kali)-[/home/kali]

└─# ls

abc  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 

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.

 

┌──(root㉿kali)-[/home/kali]

└─# rm abc

rm: cannot remove 'abc': Is a directory

But, if we use rm -d option, then we can remove the directory.

┌──(root㉿kali)-[/home/kali]

└─# rm -d abc

┌──(root㉿kali)-[/home/kali]

└─# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 

Now, let’s create our abc directory again and then remove it with rm command.

 

┌──(root㉿kali)-[/home/kali]

└─# rmdir abc

┌──(root㉿kali)-[/home/kali]

└─# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 

This time, we will create a directory with files under it. Our directory will not be an empty this time.

 

┌──(root㉿kali)-[/home/kali]

└─# mkdir abc

┌──(root㉿kali)-[/home/kali]

└─# ls

abc  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

┌──(root㉿kali)-[/home/kali]

└─# cd abc

┌──(root㉿kali)-[/home/kali/abc]

└─# pwd

/home/kali/abc

┌──(root㉿kali)-[/home/kali/abc]

└─# touch xyz

┌──(root㉿kali)-[/home/kali/abc]

└─# touch xyz2

┌──(root㉿kali)-[/home/kali/abc]

└─# touch xyz3

┌──(root㉿kali)-[/home/kali/abc]

└─# ls

xyz  xyz2  xyz3

┌──(root㉿kali)-[/home/kali/abc]

└─# cd ..

┌──(root㉿kali)-[/home/kali]

└─# ls

abc  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 

We can see the files of this directory hierarchically with tree command.

 

┌──(root㉿kali)-[/home/kali]

└─# tree abc

abc

├── xyz1

├── xyz2

└── xyz3

 

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.

 

──(root㉿kali)-[/home/kali]

└─# rmdir abc

rmdir: failed to remove 'abc': Directory not empty

┌──(root㉿kali)-[/home/kali]

└─# rm abc

rm: cannot remove 'abc': Is a directory

 

Now, let’s try with rm -r command.

 

┌──(root㉿kali)-[/home/kali]

└─# rm -r abc

┌──(root㉿kali)-[/home/kali]

└─# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 

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.

 

┌──(root㉿kali)-[/home/kali]

└─# rm -r -v  abc

removed 'abc/xyz1'

removed 'abc/xyz3'

removed 'abc/xyz2'

removed directory 'abc'




┌──(root㉿kali)-[/home/kali]

└─# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 

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.

 

──(root㉿kali)-[/home/kali]

└─# rm -r -i abc

rm: descend into directory 'abc'? y

rm: remove regular empty file 'abc/xyz1'? y

rm: remove regular empty file 'abc/xyz3'? y

rm: remove regular empty file 'abc/xyz2'? y

rm: remove directory 'abc'? y

 


 

rmdir command options

We can also sue different options with Linux rmdir command. These options are given below:

 

  • -p: Removes directories and its ancestors
  • -v: Provides a verbose list step by step.
  • –ignore-fail-on-non-empty: Does not give error for empty files.

remove-directory-linux-ipcisco.com-1

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.

 

┌──(root㉿kali)-[/home/kali]

└─# ls

abc  abc1  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

┌──(root㉿kali)-[/home/kali]

└─# rmdir -v abc*

rmdir: removing directory, 'abc'

rmdir: removing directory, 'abc1'

┌──(root㉿kali)-[/home/kali]

└─# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 

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:

 

┌──(root㉿kali)-[/home/kali]

└─# tree abc

abc

└── xxx

──(root㉿kali)-[/home/kali]

└─# rmdir abc/xxx abc

┌──(root㉿kali)-[/home/kali]

└─# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 

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.

 

┌──(root㉿kali)-[/home/kali]

└─# rmdir -p abc/xxx

┌──(root㉿kali)-[/home/kali]

└─# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

 


 

Conglusion

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!

 


Full Kali Linux Course!


 

Back to: Kali Linux Course

Leave a Reply

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