How to Remove a Directory in Linux with rm & rmdir Commands

Introduction

Removing a directory in Linux is straightforward with the GUI. However, if the GUI isn’t available, you can also delete directories using terminal commands.

This tutorial will guide you on how to remove a directory in Linux using terminal or command line commands.

Prerequisites

  • A system running a Linux distribution.
  • An account with sudo privileges.
  • Access to the terminal window or command line.

How to Delete a Directory in Linux?

Removing Directories with Terminal Commands

There are two primary commands in Linux for removing directories via the terminal or command line:

  • rm: This command removes entire directories, including all subdirectories and files within them.
  • rmdir: This command removes only empty directories.

It’s important to understand that both rm and rmdir permanently delete directories without moving them to the Trash. This means you cannot easily restore a directory once it’s removed with these commands.

Note: Although rm and rmdir permanently remove files and directories, skilled users may still be able to recover some deleted files.

1. Delete a Directory in Linux Using the rm Command

The rm command in Linux is used to remove files and directories.

The syntax is as follows:

rm [options] [file or directory name]

Note: To remove multiple files or directories using the rm command, list the names separated by spaces.

Here are the different rm command options:

  • -f: Forces the removal of all specified files or directories.
  • -i: Prompts for confirmation before each removal.
  • -I: Prompts once before removing more than three files or when removing recursively.
  • -r: Recursively removes directories and their contents.
  • -d: Removes empty directories.
  • -v: Provides verbose output, showing the removal process.
  • –help: Displays the help text.
  • –version: Displays the command version.

Attempting to use the rm command without any options to remove a directory results in an error.

An error message displays when you try to remove a directory using the rm command without options

If you want to remove an empty directory, add the -d flag to the rm command:

rm -d Example

Note: If you need to remove a directory whose name starts with a hyphen (-), use the syntax rm -- [directory name] or rm ./[directory name].

The example below demonstrates how the rm command with the -d flag removes the Example directory:

Removing an empty directory using the rm command

Use the -r flag to delete a directory that contains subdirectories and files.

The image below illustrates the tree hierarchy of the Example directory, which includes the Dir1 and Dir2 subdirectories, each containing multiple text files:

An example of a directory and file hierarchy

Using the -r flag removes the entire directory, including all subdirectories and files. Adding the -v flag provides a detailed output, listing each step of the removal process:

rm -r -v Example
Recursively removing multiple directories, subdirectories, and files using the rm command

The -i option displays a prompt asking for confirmation before removing the directory. Type Y and press Enter to confirm.

rm -d -i Example
Displaying a prompt as the output when removing a directory

Creating a write-protected directory necessitates user input during deletion. Create such a directory with the following command:

sudo mkdir Example

To remove the directory, use:

rm -d Example
rm -d write protected directory

To confirm deletion, type Y and press Enter. To bypass the confirmation, utilize the -f flag or elevate the command privileges to sudo:

rm -d -f Example
sudo rm -d Example

If the write-protected directory contains other files and directories, use the following command:

rm -rf <directory name>

Note: Executing rm -rf / is an extremely dangerous Linux command as it forcibly deletes files recursively starting from the root directory, potentially rendering your system unusable.
Exercise caution when using the -f flag and sudo while removing directories, unless you fully understand the consequences.

2. Delete a Directory in Linux Using the rmdir Command

The Linux rmdir command removes empty directories only. The command uses the following syntax:

rmdir [options] [directory name]

The rmdir command offers the following options:

  • –ignore-fail-on-non-empty: Suppresses error messages when attempting to remove a non-empty directory.
  • -p: Deletes the directory and its parent directories if they are empty.
  • -v: Outputs the process verbosely.
  • –help: Displays help information.
  • –version: Shows the command version.

Attempting to use the rmdir command on a non-empty directory results in an error.

Using the rmdir command on a non-empty directory

In this case, the Example directory contains the Test subdirectory:

Showing the hierarchy of the Example directory

To delete these directories using the rmdir command, add them in reverse order of hierarchy. Utilizing the -v option will display each step of the process as the output:

rmdir -v Example/Test Example
Removing multiple directories with the rmdir command

A simpler approach is to utilize the -p option followed by the subdirectory’s name. This removes both the subdirectory and its parent hierarchy:

rmdir -p -v Example/Test
Removing a subdirectory and its parent using the rmdir command

The rmdir command permits you to delete multiple directories with similar names using wildcards. For example, to remove directories named Example1, Example2, and Example3:

rmdir -v Example*
Using wildcards with the rmdir command to remove multiple directories

Conclusion

Upon completing this tutorial, you’ll be equipped to delete directories in Linux using commands in the terminal window or command line.

Leave a Comment