To permanently remove a directory in Linux, use either rmdir or rm command:
- For empty directories, use
rmdir [dirname]
orrm -d [dirname]
- For non-empty directories, use
rm -r [dirname]
Before you remove a directory, you need to know the name of it. To discover files and directories, use the ls command, and to know the current directory you are in, use the pwd command.
The options you use together with these commands are also important to determine how they work. Here’s a quick recap of rm command options:
Command and Option | Description |
rm -d | Remove an empty directory using the rm command. |
rm -r | Remove a non-empty directory and its content. |
rm -f | Ignore any prompt when deleting a write-protected file. |
rm -rf | Ignore any prompt when deleting a write-protected non-empty folder. |
rm -i | Output a prompt before deleting every file. |
rm -I | Output a prompt only once before deleting more than three files. |
rm * | Wildcard that represents multiple characters. |
rm ? | Wildcard that represents a single character. |
rmdir -p | Remove an empty subdirectory and its parent directory. |
rmdir -v | Print the information that the specified directory was deleted. |