How to use the Linux rmdir command plus essential options
In this installment of “Conquer The Command Line” we talk about how to use the Linux rmdir
command.
When ever you want to remove an empty directory, use the rmdir command.
Memory Aid: Think “remove directory”
The rmdir command removes, or deletes, a directory. To remove the directory type rmdir
followed by the name of the directory you want to remove. For instance, rmdir docs
. You can remove more than one directory by adding its name to the command like this. rmdir docs data
. This would remove both directories.
You can also remove a directory inside another one. If you had an empty directory called archive inside the docs directory, you could type rmdir docs/archive
. rmdir would remove the archive directory, but keep the docs directory.
Remember that for this command to work, unless you use options, the directory or directories must be empty.
Essential Options
The -p
option removes parent directories, directories closer to the root, as long as they are empty. In the previous example, you removed the archive directory and kept the docs directory. With the -p
option, you could remove the archive directory and its parent directoriy, the docs directory with this command: rmdir -p docs/archive
.
You could also remove more than one parent, as in parents of parents. All directories must be empty.
If you want to remove directories that contain files or directories, you could use other options for rmdir
to clear out all the files, or you could use the rm
command with the -rm
options. By default, Linux doesnt ask if you are sure you want to delete files and directories. It just does it. So be careful with this approach.
I prefer to take the long approach of deleting directory contents first. This forces you to think harder about removing files and directories.
Related Commands
You can make directories with the mkdir command. You can use cd to change to, or move into directories. You can use the pwd to display or print the name of the current directory. You can use the ls command to list the contents of a directory.
Remember to use the man command to find all the options for any command on your system. And remember that for a less technical explanation of commands to check out tldr pages.
What Now?
Hopefully this post has helped you take one more step in conquering the command line. I welcome any feedback.
This post was originally written at https://www.jimhomme.com