Linux is the most well-known open-source operating system and a favorite among programmers and IT experts. On Linux, novice developers frequently get into situations when they are unsure of which command to use. For instance, learning how to delete or remove a directory in Linux is one of novice engineers’ most often asked questions.
How to Remove a Directory in Linux
There are two methods in Linux for deleting a directory:
rmdir
: An empty directory is deleted by this operation.
rm command: A directory and all of its subdirectories are deleted by this command. If the folder includes any files, you must use the -r argument with this operation.
When you want to remove a folder, you might be used to dragging it to the trash on your computer. The Trash serves as a holding area for documents that you intend to erase. Before you delete the file, make sure your trash can is empty. There is no trash can on the Linux command line, which you should be aware of. It is impossible to restore a deleted file or directory.
How to Remove a Directory With the GUI in Linux
Use a file management program like Thunar (Xfce), Dolphin (KDE), or Nautilus (GNOME) to remove a directory on Linux using a graphical user interface (GUI). Depending on the file manager you’re using, the precise stages may vary slightly, but the overall procedure is as follows:
- Use the keyboard shortcut unique to your desktop environment to launch the file management program by pressing its icon in the application menu.
- Locate the directory you wish to remove by using your navigational skills. To locate the directory, utilize either the sidebar or the folder tree view.
- Right-click on the directory once you’ve found it. A context menu with many choices will then be displayed.
- Find a choice like “Delete,” “Move to Trash,” or “Remove” in the context menu. Your file manager’s specific phrasing can be different.
- To remove the directory, select the relevant option. You could be prompted with a confirmation dialogue asking whether you’re sure you want to remove the directory. After carefully reading the message, select “OK” or “Delete” to continue.
- Depending on your file manager’s settings, the directory and all of its contents will either be removed permanently or transferred to the trash.
Linux Command Line Method to Remove a Directory
While it is true that command-line interfaces (CLI) can often provide more options and flexibility compared to graphical user interfaces (GUI), the claim that CLI methods delete files and folders permanently is incorrect. Both CLI and GUI methods can be used to delete files and folders, and the permanence of the deletion depends on the specific command or action taken, not the interface itself.
To remove an empty directory instead, use the rmdir
command. Rmdir won’t function if the directory has any files or subdirectories. In such a case, you must use rm -r
.
How to Delete the Directory Using the rmdir
Command.
For the purpose of deleting empty directories, the command-line tool rmdir
is employed. It helps when you just want to remove a directory if it is empty and doesn’t want to check to see whether it is.
To delete a directory, type rmdir
followed by the name of the directory you want to get rid of. For instance, type: to remove the directory dir1
from your computer.
rmdir dir1
It will fail with the following message if the directory is not empty:
rmdir:
directory ‘dir1/
‘ could not be removed since it was not empty.
Utilize the rm command or manually remove the directory’s contents first, then delete the directory.
How to Delete Empty Directory in Linux
The rmdir
command can be used in Linux to remove an empty directory. Here’s what you should do:
On your Linux computer, launch a terminal.
The empty directory you intend to delete’s parent directory should be visited. For instance, you would go to “cd/path/to/the/file
” if the empty directory was placed at /home/user/empty dir
.
Run the rmdir
command after entering the parent directory and the name of the empty directory you want to remove. If the empty directory, for instance, is called empty dir, you would run:
rmdir emptydir
You will get an error warning, and the directory won’t be erased if it isn’t empty. You might need to use the rm
command with the option to destroy the directory and all of its contents recursively in that situation.
Rm -r, however, permanently deletes files and directories without your permission, so use it with caution.
How to Delete Non-Empty Directory in Linux
On Linux, you may use the rm command with the “recursive
” parameter (-r
) to delete a directory that is not empty. A directory and its contents may be deleted with this flag. This command may be used; simply replace “folder” with the name or location of your directory.
rm -r folder
Use the rm command with the -r parameter with caution since it deletes files and directories permanently without asking for approval. The deletion of a directory cannot be reversed, so make sure you are convinced about it.
How to Delete Directory Using the rm Command.
You may remove directories and files with the rm
command. Unlike rmdir
We’ll talk about this moment; this command may be used to delete empty and non-empty directories.
The rm command has the following syntax: rm [flags] [file/folder name]
When using rm, there are two flags that you may use to remove a directory. Which are:
-d
: An empty directory should be deleted.
-r
: Remove any and all files and directories that aren’t empty.
-f
: Forced choice. In other words, disregard invalid parameters and missing files, and never prompt. The option is risky. Be cautious.
-v
: Detailed option. Show the screen’s activity for rm.
-i
: When this option is set, the command will request the user’s approval before deleting anything.
A directory cannot be retrieved once it has been deleted. Before you remove a directory, make sure you back up any crucial data.
How to Delete Empty Directories
Use the -d parameter when running the following command in Linux to remove empty directories:
rm -d <directory_name>
How to Delete Non-Empty Directory
Removing non-empty items might be extremely risky since it may erase important data. Therefore, while removing non-empty folders, use particular care. Use the -r parameter with the following command to remove a directory along with all of its contents:
rm -r <directory_name>
How to Force Delete Directories in Linux
The rm -r
Command will, by default, request your confirmation before deleting a file or folder. When you attempt to remove a file that has been protected, this occurs. You may use the -f
switch to override this by specifying it as follows:
rm -rf lib
All folders and subdirectories in the “lib
” directory will be permanently deleted by this command. When you use this command, you won’t be asked if you really want to remove any files. Therefore, you should only use this command when you are certain that you want to erase the contents of a folder.
How to Prompt Before Deleting Folders in Linux
In Linux, there are two ways to ask permission before removing a folder:
When using the rm command, use the -i option. By doing this, each folder will first ask for your approval before being deleted.
Make an alias that uses the -i
option for the rm command. Doing this will include the prompt to every rm command you do.
Open a terminal window and go to the directory containing the folder you wish to remove to utilize the -i
option. Run the subsequent command after that: rm -i folder_name
.
A confirmation window will appear before the folder is deleted. Press Y
to remove the folder or n
to stop.
The .bashrc
file must be opened in a text editor to be given an alias for the rm command. In your home directory, you may locate this file.
The final line of the .bashrc
file should read: alias rm='rm -i'
Close the file after saving it. Open a fresh terminal window after that. Before destroying any files or directories, the rm program will request your approval.
It is crucial to keep in mind that if you are simultaneously removing numerous directories, the -i
option will not ask for your approval. Use the -I option to ask for approval before removing several directories.
rm -I folder_name1 folder_name2
How to Delete Directories Using the ‘find’ Command
A command-line tool called find looks for files and folders based on an expression that is provided and does an action on each one that matches.
Delete Empty Directory in Linux
Using the search command and the -delete element, you may also remove directories, as displayed below:
find <path_to_search> -type d -name "directory_name" -delete
This command will search for any empty directories in the supplied path that match the directory_name input and remove them.
Delete Non-Empty Directories
Use the following syntax for deleting non-empty folders using the find command:
find <path_to_search> -type d -name "directory_name" -exec rm -r {} +
The find command searches the path to search for directories matching the specified directory name in the syntax above. Subsequently, using the -exec
parameter, the search results are sent to the rm command, which subsequently uses the -r flag to destroy the directory.