Different methods are used to remove a directory in Linux. This article will discuss the possible methods to delete the directory using the terminal and Graphical User Interface methods.
How to Remove a Directory in Linux?
The directory stores the files, which can be empty or have files. To Linux directory is removed by following these two approaches:
- How to Remove a Directory in Linux Using Terminal/CLI?
- Removing Empty Directories (rmdir)
- Removing Non-Empty Directories (rm)
- How to Remove Directory in Linux Using File Manager/GUI?
Approach 1: How to Remove a Directory in Linux Using Command Line?
For removing the empty directory, the “rmdir” or “rm -d” commands are used, while the non-empty directory is deleted with the “rm -r” command.
To delete/remove a file in Linux, the general syntax of the “rmdir” command is given below:
$ rmdir [options] [directoryname]
The “rm” has the following syntax:
$ rm [options] [directoryname]
Options for rmdir and rm Commands
To check the available flags/options in the “rm” command, use the “help” option as shown below:
$ rmdir --help
The available options for “rm” are shown below:
$ rm --help
Removing Empty Directories
The “rmdir” command stands for remove directory. It is used to delete the empty directory in Linux. Let’s delete empty directories with the help of examples.
How to Remove an Empty Directory in Linux?
The vacant directory/folder can be removed in Linux utilizing the “rmdir” command as shown below:
$ rmdir testfolder1
Another method to remove the empty directory is executing the “rm -d” command as follows:
$ rm -d testfolder2
To check the status of the “rmdir” command for every directory, use the “v” option/flag as given below: (Used the “v” flag in the article to check the directory process with commands)
$ rmdir -v testfolder3
How to Remove Multiple Empty Directories in Linux?
Deleting multiple files simultaneously with a single rmdir can be done by executing the following command:
$ rmdir testfolder4 testfolder5 testfolder6
How to Remove the Directory of a Specific Name in Linux?
If you have a lot of directories and want to delete the directory with its name, use the following command:
$ rmdir -v *testfolder7*
How to Remove an Empty Sub-Directory in Linux?
To delete a sub-directory, use the below command using its complete path as performed below:
$ rm -d -v /home/ubuntu/Desktop/testfolder14/subfolder14
To delete an empty directory with all the empty subdirectories, use the “p” option of the “rmdir” command.
For deleting the empty “subfolder13” and its parent directory “testfolder13”, you can use the below command, which will not remove non-empty directories:
$ rmdir -p -v /home/ubuntu/Desktop/testfolder13/subfolder13
How to Delete All Empty Directories in Linux?
You can delete all empty directories by running the following command:
$ rmdir -v *
Removing Non-Empty Directories
The rm deletes the non-empty directories, which is done in the upcoming section.
How to Delete a Non-Empty Directory?
To remove a non-empty directory named “testfolder1”, use the below command with the “r” option to delete all the empty directories, and the “v” option is used to print the status for every line of code:
$ rm -r -v testfolder1
How to Delete Multiple Non-Empty Directories?
To delete several non-empty directories, use the “rm -r” command with the specified file names below:
$ rm -r -v testfolder2 dtestfolder3
How to Prompt Before Removing File in Linux?
If you have an important file and want to make sure the correct file is deleted, use the “i” option to get a prompt before deleting a file as given below:
$ rm -r -i -v testfolder
To permanently delete the files, write “yes” else “no”.
How to Remove a Protected Directory in Linux?
The file protection is of three types: read “r”, write “w”, and execute “x”. If the folder is write-protected and you want to delete that folder, it will give you a prompt for confirmation:
$ rm -r -v testfolder5
To delete the write-protected folder/directory, use the “f” force option of the rm command as follows:
$ rm -rf -v testfolder
Approach 2: How to Delete a Directory in Linux Using File Manager?
A Linux directory is deleted using the GUI methods with the below two easy steps:
- Select a single folder or several folders.
- Select the “Move to Trash” option to delete the folder.
Shortcut Key: Select the folders and press the “Delete” shortcut key to delete the folders.
The “rm” and “rmdir” commands are executed to remove or delete a directory in Linux. The “rmdir directory-name” & “rm -d directory-name” commands are used to remove an empty directory.
While for deleting a non-empty directory, the “rm -r directory-name” command is utilized. Several methods to delete the directory, including the GUI method, are discussed in the article.