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:
rmcommand removes complete directories, including subdirectories and files.
rmdircommand removes empty directories.
It is important to note that the
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 -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.
-r flag to delete a directory that contains subdirectories and files.
rmdir command removes empty directories only. The command uses the following syntax:
rmdir [options] [directory name]
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.