How to remove or delete a directory in Linux – Removing a directory from the Linux machine could be a bit confusing for many users. Especially, the one who is new to Linux operating system or trying to learn it.
However, a user can remove a directory from a Linux operating system using GUI (Graphical user interface). Yes, It is possible to delete a directory using your mouse in Linux also just like windows, if you use desktop file manager. But if you are a Linux OS user and using GUI to delete life in Linux Machine then It is better to use a windows machine.
Table of Contents
How Remove or Delete Directory in Linux
In Linux operating system there are many different ways to delete or remove a directory from the machine. The Linux users who use KDE’s Dolphin or Gnome’s file can easily delete a directory from a Linux machine by just using their mouse.
But what if you are working on a headless server where there is no option to use file manger?
Well, no problem; you can do anything in Linux using commands. So, here we are going so show you how you can delete or remove a directory from a Linux machine using command line interface.
rm and rmdir are command to delete or remove directory from Linux.
You Must Know It Before Deleting a Directory
Before you run any Linux command to delete a directory, It is important to know the terms of command. Some people visit to our website copy the command and try to delete files or directories without knowing about the complete use of the command.
When you delete a directory from your Linux machine using the command-line interface, it would be almost impossible to recover it again. So, be a bit careful before you run the command.
However, if you are using Gnome’s file or KDE Dolphin then the file you delete gets stored into the Trash. As long as any file or directory is into the Trash, It could be recovered anytime. But once you delete the file or directory from the Trash also then it is not possible to recover the file or folder without using any extra file recovery software.
While Delete a Directory in Linux you may get some errors like “Operation not permitted” this error occurs when you try to delete a folder or directory in Linux which do not have write permission.
So, if you get such kind of error then you will have to give write permission to the director and its contents.
Remove Empty Directory or Folder Using rmdir Command
In Linux, rmdir is a command which is used to delete the Directory or Folder that is empty. rmdir is of course a command to delete a directory in Linux operating system but this command does not work for delete a directory or a folder that contains any kind of content or file.
How to use rmdir to delete directory?
It is super simple to use rmdir command but sometimes it creates confusion for the new users. They try to delete any folder/directory using this command without knowing that it can not delete a directory that has any type of file.
rmdir: failed to remove 'DirectoryName': Directory not empty
This the error which occurs when you try to delete directory which is not empty.
Well, this is how you use rmdir command-
use rmdir command followed by the name of the directory which you want to delete or remove.
For Example, there is a “NewProject” Directory in my machine which contains nothing.
Here, Notice – The “NewProject” directory is on my Desktop. To delete “NewProject” I will have to open my Desktop in the command line interface.
To do so, after opening the command line interface first of all run “pwd” command to know the current path of the command line.
By default your current open directory in command line would be the home/user.
Now to delete “NewProject” Directory I need to get into Desktop. So, what I will do is-
Run cd Desktop command-
Now, I can easily delete “NewProject” directory just by running rmdir command like this.
After running rmdir command followed by the directory name the directory gets removed but the condition is the directory should be empty.
How to remove directory/folder using rm command in Linux?
If you want to remove a Directory in Linux which contains files or any kind of Data rmdir can remove it.
So, to remove such directories you need rm command. rm is one of the command line utility which is used to delete file or directory in Linux operating system.
rm command can delete both empty and non-empty directories.
But without proper knowledge about the rm command, It would be difficult for the users to use it.
Here, is how to use rm command-
The rm command is used with some options like -d, –dir and -r. If you want remove an empty directory with rm command then you will have to run rm -d or rm –dir and if you want to delete non-empty folder then you will have to run rm -r command.
Run this command to delete an empty folder/directory-
rm -d NewProject
rm --dir NewProject
Note : NewProject is a directory name on my computer, In your case, it could be anything.
Deleting Non-Empty directory in Linux Using rm Command-
rm -r NewProject
If i run this command the directory named NewProject will get delete along with contents within it.
Sometimes you might be asked to confirm the deletion if the Directory or File inside the directory is write-protected.
So, In such a case you will have to use rm -rf option.
rm -rf NewProject
How to delete multiple directories at once?
You can simply delete multiple empty or non-empty directories at once.
Use rm -r or rm -rf commands followed by the directories names separated with blank space.
Like there are three directories in my computer- NewProject, OldProject and CurrentProject. I want to delete all these at once. So, here is the command I will have to run-
rm -rf NewProject OldProject CurrentProject
Some Extra Tips For New Linux Users
For the new users Linux could be a bit difficult to use. Linux is mostly known for its powerful command line interface also known as terminal.
To use the terminal, one should need to know the commands. For performing every other operation there is a special command for it. So, here will talk about some Linux commands that are directly or indirectly associated with file management.
How to find list of directories inside a directory in Linux?
A directory or a folder contains other directories or files inside it. When you use GUI you can easily check what files or folders are inside any folder/directory by just opening it.
But when you use command line interface you need to know the command to check the list of the directories or files inside directory.
So, we ls command to check the list inside a directory.
Suppose, I want to check the list of directories and files inside my home folder then I will have to run ls command inside my home directory.
Desktop Documents Downloads Music Pictures Public snap Templates Videos
If you want to see list of directories and files in table format then you will have to run following command-
drwxr-xr-x 3 factforgeeks factforgeeks 4096 Jan 31 16:31 Desktop drwxr-xr-x 2 factforgeeks factforgeeks 4096 Apr 1 2020 Documents drwxr-xr-x 5 factforgeeks factforgeeks 4096 Jan 30 17:34 Downloads drwxr-xr-x 2 factforgeeks factforgeeks 4096 Apr 1 2020 Music drwxr-xr-x 2 factforgeeks factforgeeks 4096 Jan 31 16:31 Pictures drwxr-xr-x 2 factforgeeks factforgeeks 4096 Apr 1 2020 Public drwxr-xr-x 4 factforgeeks factforgeeks 4096 Oct 25 07:25 snap drwxr-xr-x 2 factforgeeks factforgeeks 4096 Apr 1 2020 Templates drwxr-xr-x 2 factforgeeks factforgeeks 4096 Apr 1 2020 Videos
Sometimes you might want to delete some hidden directories or files in Linux. So how will you do it?
Well, It is a very simple process.
Run ls -la command to see hidden documents and then remove them using rmdir or rm command.
There are many ways to remove a directory in Linux operating system. We have covered almost all in this post. Here, in this post you have learned how to remove a directory in Linux using rm and rmdir commands. As well as, we have also explained how to use some options with rm like -r and -d.