How to Remove a Directory in Linux {rm & rmdir Commands)

Introduction

Deleting a directory on Linux is a fairly simple task if you are using the GUI. However, if you don’t have access to the GUI, you can also delete directories using terminal commands.

In this tutorial, we will show you how to delete a directory on Linux through commands in the terminal window or command line.

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 on Linux? There are two Linux commands you can use to delete a directory from

  • the terminal window or

command

line:

  • The rm command deletes entire directories, including subdirectories and files.
  • The rmdir command removes empty directories.

It is important to note that the rm and rmdir commands permanently delete directories without moving them to the Trash directory. This means that you cannot restore a deleted directory by using these commands.

rm command The rm

command

on Linux deletes files and directories.

It uses the following syntax

: rm [options] [file or directory name]

The different rm command options include

: -f: Force the deletion of all files or directories. -i: Prompt for confirmation before deleting. -I:

  • Ask once before deleting more than three files or when deleting recursively
  • .
  • -r : Deletes directories and their contents recursively.
  • -d: Deletes empty directories.
  • -v: Provides detailed output.
  • -help: Displays the help text.
  • -version: Displays the version of the command.

When you try to use the rm command without any option to delete a directory, you receive an error message: If you want to delete an empty directory, add the -d flag to the rm command: rm -d Example

The following example shows that the rm command

with the -d flag removes the Example directory:

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

The

following image shows the tree hierarchy of the Example directory, which contains Dir1 and Dir2

subdirectories, with multiple text files in each: Using the -r flag deletes the entire directory,

including subdirectories and files, while the -v flag lists each step of the process as output: rm –rv

Example

The -i option Displays a message asking you to confirm the deletion of the directory. Type Y and press Enter to confirm.

rm -d -i Example

Write-protected directories require user intervention when deleting. Create such a directory with

: sudo mkdir Example

To delete the directory, use:

rm -d Example

Type Y and press Enter to confirm the deletion. To avoid confirmation, use the -f flag or raise command privileges for

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> rmdir command The

Linux rmdir command only deletes empty directories.

The command uses the following syntax: rmdir [options] [directory name]

The rmdir command includes the following options

: –

  • ignore-fail-on-non-empty: Does not display an error message when attempting to delete a non-empty
  • directory. –

  • p: Deletes the directory along with its parent in the hierarchy
  • . –

  • v: Provides detailed output. –
  • help

  • : Displays the help text.
  • -version: Displays the version of the command.

Using the rmdir command on a

non-empty directory fails

:

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

remove these directories by using the rmdir command, add them in reverse hierarchy order. Using the -v option lists each step of the process as output:

rmdir -v Example/Test Example

A simpler method of doing this is to use the -p option with the name of the subdirectory. This deletes both the subdirectory and its hierarchical parent:

rmdir -p -v Example/Test

The rmdir command allows you to delete multiple directories with similar names using wildcard characters. For example, if you want to delete directories named Example1, Example2, and Example3:

rmdir -v Example*

Conclusion

After reading this tutorial, you should be able to delete directories on Linux using commands in the terminal window or command line

.

For more information on other commands on Linux, see our Linux command cheat sheet.