How to Remove a Directory in Linux?
There are two Linux commands you can use to remove a directory from the terminal window or command line:
- The
rm
command removes complete directories, including subdirectories and files. - The
rmdir
command removes empty directories.
It is important to note that the rm
and rmdir
commands permanently remove directories without moving them to the Trash directory. This means that you cannot restore a directory removed using these commands.
If you want to remove an empty directory, add the -d
flag to the rm
command:
rm -d Example
If you want to remove a directory whose name starts with a hyphen (–), use the rm -- [directory name]
or rm ./[directory name]
syntax.
Use the -r
flag to delete a directory that contains subdirectories and files.
rmdir Command
The Linux rmdir
command removes empty directories only. The command uses the following syntax:
rmdir [options] [directory name]
The rmdir
command includes the following options:
--ignore-fail-on-non-empty
: Doesn’t show an error message when trying to remove a non-empty directory.-p
: Removes the directory along with its parent in the hierarchy.-v
: Provides a verbose output.--help
: Displays help text.--version
: Displays the command version.