How to remove a directory in Linux – Commands with examples

remove a directory in Linux

Here is a tutorial on how can we delete or remove a directory in Linux. Linux is a very powerful open-source operating system. Almost all programmers want to learn it. There are a lot of resources available on the internet by which you can easily learn Linux.

And there is also a big community of it so a new developer will not face any difficulty in learning the Linux operating system.

We call everything as a file including a directory in the Linux world. The directory is the combination of files. In windows and mac os world we call the directory as a folder.

There are a couple of ways by which you can delete a directory or file in Linux. The first one is, you can delete a directory by GUI. and there is no need for any commands. Another one is the command-line tool which is more efficient and will give you a real feel of the Linux operating system.

If you delete a file graphically then it will go into the trash and someone can recover it. But if you want to delete a file permanently then you need to fire a couple of commands in the command-line tool.

Let’s discuss some commands that are used to remove the directory in Linux.

Commands that are responsible for deleting a directory in Linux.

  • rmdirresponsible for deleting the specified empty directory.
  • rmresponsible for deleting the directory including subdirectories.

rmdir command to delete a directory in Linux

The rmdir command will remove all the directories or a directory if they are empty.

The syntax of the command :

~$rmdir directory_name 

~$rmdir [option] directory_name

For example,

Suppose we want to delete a directory name examples then we need to write the command like this.

~$ rmdir examples

If we want to delete a directory called /temp/docs then we need to perform operations like this.

~$ rmdir /temp/docs

If the specified directory is not empty then you will get an error message like this.

~$ rmdir videos

Output :

~$ rmdir: videos: directory not empty

In this scenario, you need to navigate into the videos directory and fire the command called ls to see the list of flies and then delete those files.

~$ cd videos

~$ ls

Now let’s try to see some messages while making an operation on the directory.

If you want to see some messages related to the operations, you need to pass -v option along with the rmdir command.

For example :

~$ rmdir -v examples

Output :

rmdir: removing directory: “examples”

Removing the directories with the help of some wildcards:

We can use ‘*’ and ‘?’ wildcards for deleting the multiple directories.

For example :

For deleting all the directories whose name started from the ‘example’ we need to go like this.

~$ rmdir -v example*

Output :

~$ rmdir: removing directory, ‘example_1’

~$ rmdir: removing directory, ‘example_2’

~$ rmdir: removing directory, ‘example_3’

~$ rmdir: removing directory, ‘example_4’

~$ rmdir: removing directory, ‘example_5’

rm command for removing entire directory along with subdirectories in Linux:

The rm command is used to delete all the directories and subdirectories in Linux.

~$ rm -rf letters/
  • r – r is responsible for removing the file hierarchy rooted in each file.
  • f – f is responsible for removing the file without any warning prompt.

Problems in deleting a directory:

Only the superuser or owner can delete his directory or any other directory. If you want to delete a directory normally then you will get an error message. So make sure you type the Sudo command along with your removing command.

For example :

~$ Sudo rmdir /Desktop/dir_1

~$ Sudo rm -rf dir_2
  • Suppose if you want to delete all the directory named ‘example’.

Then you need to fire commands like this.

~$ find . -type d -iname ‘example’ -delete
  • Deleting all the empty directories.
~$ find . -type d -iname ‘example’ -empty -delete

Explanation :

  • type dthis command ignores all the files and only search for the directory.
  • iname ‘example’Search only for example directory.
  • delete delete the specified directory.

 Deleting .DS_Store directory:

If you want to delete all the .DS_store files that are stored in  ‘/desktop/www/html’ then you need to fire these commands.

~$ sudo find /desktop/www/html/ -type d -name .DS_store -exec rm{} \

Or

~$ sudo find /desktop/www/html/ -type d -name .DS_store -exec rm{} +
  • -exec = option to find an external command rm.
  • rm{} + = its a good option to delete all the .DS_store files.

Conclusion on how to remove a directory in Linux

In this article, we have discussed a couple of commands by which you can delete a directory that you want to delete whether its an empty directory or a combination of directories.

Linux also provides a GUI by which you can easily delete a directory without firing a command but this method is not efficient because GUI based process moves the directory to the trash and someone can recover it. while the command-based process deletes your directory permanently.

Leave a Comment

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

Scroll to Top