“The rm and rmdir both are built-in command line utilities that come in handy when you need to remove directories. Both are part of the GNU core utilities that allow a user to clean their disk space by removing the directories that are no longer needed. While both the utilities allow removing the directories, the rmdir only allows removing “empty directories”. It prevents accidentally removing the non-empty directories. If you try to use the rmdir command to remove a non-empty directory, it will throw the “Directory not empty” error message. If you want to remove a non-empty directory, you can use the rm command.”
Another thing to remember is that when a file or directory is removed using either the rm or rmdir, it is immediately removed from the system rather than being sent to the Trash. Hence, you must be very careful while using these commands, as you will not be able to retrieve the removed files and directories without a backup.
In this article, we are going to show how you can use the rmdir command to remove directories in Linux, along with some examples. We have demonstrated the examples on Linux Ubuntu. However, these work the same on every Linux distribution.
Syntax of Rmdir Command
The syntax of the rmdir command is as follows:
To launch the Terminal, click the Activities tab on the top left of the screen. Then type terminal or shell to search for it in the applications menu. You can also use the Ctrl-Alt-T default keyboard shortcut.
Remove a Directory Using Rmdir
To remove a single empty directory, type rmdir followed by the directory name or path to the directory as follows:
This command will delete the “~/Documents/myfiles” only if it is empty. If the target directory is not empty and contains some files, the rmdir command will throw the “Directory not empty” error message.
Remove Multiple Directories Using Rmdir
You can also remove multiple directories at once with the rmdir command. Let’s say to remove three directories named “~/Documents/myfiles”, “images”, and “games”; the command would be:
This command will remove the “~/Documents/myfiles”, “images”, and “games” directories only if they are empty.
Verbose Output
To see what happened while using the rmdir command, use the -v option with the rmdir command:
For all the next examples, we will be using the -v option to see what is happening in the background when we run the rmdir command on a directory.
Suppress Fail on Non-Empty Message
As discussed above, the rmdir does not remove the empty directories. Instead, it fails and displays a “Directory not empty” message. The –ignore-fail-on-non-empty option allows you to suppress the message. However, note that it still does not remove the non-empty directory.
Let’s say we want to remove three directories named “games”, “videos”, and “images,” where “games” and “images” directories are empty while the “videos” directory is non-empty.
It is visible in the following output that the rmdir command has removed the “games” and “images” directories but failed to remove the “videos” directory and displayed the “Directory not empty” error.
Using the rmdir command with the –ignore-fail-on-non-empty option will suppress the error message; however, it will not remove the non-empty directory.
Remove Directory and its Parent Directories
Using the rmdir command, you can also remove the hierarchy of directories, which means you can remove directories and their parent directories. Let’s say we have a hierarchy of three directories docs, myfiles, and files such that docs are inside myfiles and myfiles are inside files.
Now to remove the hierarchy, you will have to remove the directory and its subdirectories using this command:
As an alternative, you can use the -p option with the rmdir command to remove the hierarchy as follows:
This command will remove the “docs” directory along with its parent directories “myfiles” and “files”. The -p option tells the rmdir command to start with the target directory “docs” and the back steps to its parents.
Note: Remember, the rmdir command only removes the empty directories. Therefore, the directory files should not have anything other than the myfiles directory, and the directory myfiles should not have anything other than the docs directory.
To view more information about the rmdir command, you can see its man page using the command below:
Once done, press q to exit the manual page.
On the other hand, you can use the command below to view help for the rmdir command:
Note: Removing Non-Empty Directories
As we have stated earlier, that rmdir command does not allow removing a non-empty director. However, you can use the rm command for this purpose:
Conclusion
The rmdir command provides some protection against accidentally deleting files as it only deletes the directories that are empty. In this article, we covered the use of the rmdir command in Linux, along with some examples.
from https://ift.tt/gDFUCqw
0 Comments