Updated April 18, 2023
Introduction to Remove dir Linux
We know that Linux system is made up of files and directories. There may be plenty of directories in our system which is no longer needed. So we need to clean them up periodically to free up space. In Linux there are commands like Remove dir Linux, rm and find commands which are used to remove directories from our Linux file system. In this article we will discuss about how to remove directories in Linux with various command line options.
Commands used to Remove Dir Linux
The commands rmdir, rm and find are used to remove or delete the directories from our Linux system. Let us discuss them one by one.
Rmdir Command
The rmdir command is used to remove empty directories from our Linux Operating System. Every directory that needs to be removed should not contain any files or subdirectories. Otherwise, rmdir command cannot remove the directory from our file system. The directories are removed in the order that they are specified in the command line i.e. from left to right. To remove both the parent and subdirectory of the parent, the subdirectory should be mentioned first so that the parent directory is made empty and rmdir can remove it.
Syntax
The syntax of the rmdir command is given below.
rmdir [-p] [-v, --verbose] [--ignore-fail-on-non-empty] [ directory names....]
Explanation: Here p, v and ignore-fail-on-non-empty are options used in the rmdir command.
Examples to Implement Remove dir Linux
Below are the examples mentioned:
Example #1
Code:
rmdir sampledir1
Exaplanation: The above command will remove the directory named sampledir1 if it is empty. If it is not empty then the error message will be produced. In this case you have to remove the directory contents before deleting it.
Example #2
To remove multiple directories at once use the following command.
Code:
rmdir sample1 sample2 sample3
Output:
Exaplanation: The above command will remove all the directories sample1 sample2 and sample 3 if they are empty. If any of the directories is not empty then an error message will displayed for that particular directory and other directories will be removed from the file system.
How to Ignore Non-Empty Directories?
By default, if you are trying to remove a non-empty directory, the rmdir command will produce an error message. To ignore the error message that arises on deleting a non-empty directory, use the –ignore-fail-on-non-empty option.
Code:
$ rmdir--ignore-fail-on-non-empty test-dir
Output:
How to Remove Parent Directories using Remove dir Linux?
rmdir command can also perform its operation over the parent directories. It has the capacity to delete both the directory and its parent directories in one shot. You can make use of this feature with the help of-p option.
Code:
$ rmdir -p test/testdir/
Explanation: The above command will delete both test and testdir directories. For this to happen, all parent directories should be empty other than the empty directory to be deleted.
How to get Help for Remove dir Linux?
To get help about the rmdir command use the following command.
Code:
$ rmdir --help
RM Command
rm is an utility command used for deleting both files and directories. The rm command can be used for deleting both empty and non-empty directories.
By default, if the rm command is used without any options it does not delete directories. Use the -d option to remove an empty directory.
If you desire to remove a non-empty directory, then you can use rm command with the recursive (-r or –R)option.
Code:
$ rm [options] [directory name]
Using RM Command to Remove Directories
To remove a directory named sampledir and all of its contents use the following command.
Code:
$ rm - r sampledir
If the file or directory is write protected, then you will be prompted for confirmation. To remove the directory without the prompt message, use the -f option as shown below.
Code:
$ rm –rf sampledir
Output:
Explanation: To delete multiple directories in one go,type the name of the directories with each directory being separated by a space.
$ rm -r one two three
If you use the -i option you will be prompted for confirmation before deleting each file or directory. If the directory contains lot of files and subdirectories, it can be irritating. In that case we can use the -I option which prompts the user only once before deletion.
$ rm –rI sampledir
You will be prompted once. To delete the directory, type yand press Enter key. We can also use regular expressions to delete directories. For example, If you want to remove all directories in the present directory that ends with _ing, you can use the following command.
$ rm -r *_ing
Find Command
The find command searches for a file in the current working directory and recursively through the subdirectories that matches the given search criteria and performs the given action on the matched file or directory. You can search files by name, extension, group, modification date, permissions etc.
The find command is also used to remove directories that match the given pattern. For example, To remove all directories that end with _tmp in the current directory, use the following command.
Code:
$ find . -type d -name '*_tmp' -exec rm -r {} +
Explanation:
- (.) –Itsearches recursively in the current working directory.
- -typed – searches only the directories.
- -name’*-tmp’ – searches only the directories that end with _tmp
- -exec – It executes an external command rm with the option -r.
- {}+ – appends the searched directories at the end of the command rm.
How to Remove all Empty Directories?
To remove all the empty directories, run the following command.
Code:
$ find /dir -type d -empty -delete
Explanation:
- /dir – starts searching from the /dir directory and all sub-directories beneath the /dir directory.
- -typed -searches only the directories.
- -empty – searches only the empty directories.
- -delete – It deletes all the resulting empty directories found.
-delete option should be used with extreme caution. If the -delete option is specified first, then the command will remove all directories below the starting point we specified.
Conclusion
In this article, we have discussed about how to remove directories from our Linux system using the various command line options. Delete the directories with utmost precaution because once removed it is almost impossible to recover the files in the Linux system.
Recommended Articles
We hope that this EDUCBA information on “Remove dir Linux” was beneficial to you. You can view EDUCBA’s recommended articles for more information.