How to Remove a Directory in Linux

October 21, 2021

Introduction

Removing a directory in Linux is a pretty simple task if you are using the GUI. However, if you don't have access to the GUI, you can also remove directories using terminal commands.

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

How to remove directories in Linux

Prerequisites

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

Note: For other Linux directory management articles, see How to Move Directories in Linux and How to Rename a Directory in Linux.

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.

Note: Even though rm and rmdir permanently remove files and directories, users with enough skill and time still have a chance of restoring some of the removed files. If you want to learn more about removing files permanently, have a look at our shred command tutorial.

rm Command

The rm command in Linux removes files and directories.

It uses the following syntax:

rm [options] [file or directory name]

Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces.

The different rm command options include:

  • -f: Forces the removal of all files or directories.
  • -i: Prompts for confirmation before removing.
  • -I: Prompts once before removing more than three files or when removing recursively.
  • -r: Removes directories and their content recursively.
  • -d: Removes empty directories.
  • -v: Provides a verbose output.
  • --help: Displays the help text.
  • --version: Displays the command version.

Trying to use the rm command without any options to remove a directory results in an error message:

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 want to remove a directory whose name starts with a hyphen (-), use the rm -- [directory name] or rm ./[directory name] syntax.

The example below shows that 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 shows the tree hierarchy of the Example directory, which contains Dir1 and Dir2 subdirectories, with multiple text files in each:

An example of a directory and file hierarchy

Using the -r flag removes the entire directory, including subdirectories and files, while the -v flag lists each step of the process as the output:

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

The -i option displays a prompt asking you to confirm directory removal. Type Y and press Enter to confirm.

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

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.

Using the rmdir command on a non-empty directory produces 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 remove these directories using the rmdir command, add them in reverse order of hierarchy. Using the -v option lists each step of the process as the output:

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

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

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

The rmdir command allows you to remove multiple directories with similar names using wildcards. For instance, if you want to remove directories named Example1, Example2, and Example3:

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

Conclusion

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

To learn more about other commands in Linux, check out our Linux commands cheat sheet.

Was this article helpful?
YesNo
Aleksandar Kovačević
Aleksandar Kovacevic is an aspiring Technical Writer at phoenixNAP. With a background in both design and writing, he aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Extract or Unzip tar.gz Files from Linux Command Line
November 14, 2019

This article shows which commands best to use when compressing and decompressing files from the command line. By using the two compression utilities, covered in this tutorial, tar and gzip, you can manage zip files with ease.
Read more
Linux curl Command Explained with Examples
September 16, 2021

Linux offers multiple tools for data transfer to and from a server, the most popular being curl and wget. This tutorial will show you how to use the curl command...
Read more
Linux Source Command with Examples
July 22, 2021

The Linux source command allows you to execute a list of commands from a text file or script. Learn the different ways you can use the source command in this tutorial.
Read more
man Command in Linux with Examples
March 31, 2021

The man command is a built-in Linux utility that allows users to search for any available command and displays a user manual for the specified command. It features the command description, examples, and acceptable options.
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.