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

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 a Directory in Linux {rm & rmdir Commands) (1)

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 Delete 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.

1. Delete a Directory in Linux Using the 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:

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

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:

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

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:

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

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
How to Remove a Directory in Linux {rm & rmdir Commands) (5)

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

rm -d -i Example
How to Remove a Directory in Linux {rm & rmdir Commands) (6)

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

sudo mkdir Example

To remove the directory, use:

rm -d Example
How to Remove a Directory in Linux {rm & rmdir Commands) (7)

Type Y and press Enter to confirm deletion. To avoid the confirmation, use 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: rm -rf / is a dangerous Linux command that forces a recursive deletion on the root directory, rendering your system unusable.
Avoid using -f and sudo while removing directories, unless you know what you're doing. To learn more about this command, visit our article What Is sudo rm -rf in Linux and Is It Dangerous.

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 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:

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

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

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

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
How to Remove a Directory in Linux {rm & rmdir Commands) (10)

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
How to Remove a Directory in Linux {rm & rmdir Commands) (11)

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*
How to Remove a Directory in Linux {rm & rmdir Commands) (12)

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.

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

References

Top Articles
Sun Stone Pokemon Infinite Fusion
Wells Fargo Careers Log In
No Hard Feelings Showtimes Near Metropolitan Fiesta 5 Theatre
Gfr Soccer
Sams Gurnee Gas Price
Lesson 10 Homework 5.3
Nene25 Sports
Phun.celeb
Lovex Load Data | xxlreloading.com
The 8 Best Santa Ynez Wineries to Visit in 2023
Ups Open Today Near Me
Nook Glowlight 3 Case
iPad 10 vs. iPad Air Buyer's Guide: Is the $250 Difference Worth It?
2016 Hyundai Sonata Refrigerant Capacity
Generation Zero beginner’s guide: six indispensable tips to help you survive the robot revolution
Scholar Dollar Nmsu
Wow Patchu Pet Battle
Comcast Business Sign In
Craigslist Siloam Springs
Dmv Leestown Rd
Video Program: Intermediate Rumba
10 Teacher Tips to Encourage Self-Awareness in Teens | EVERFI
Robert Rushing Net Worth, Daughter, Age, and Wikipedia
Jennette Mccurdy Cameltoe
Hyb Urban Dictionary
Jordan Torres Leaked
ASVAB Test: The Definitive Guide (updated 2024) by Mometrix
Movierulz.com Kannada 2024 Download: Your Ultimate Guide
Configuring TPM 2.0 on a 6.7 ESXi host
Sold 4 U Hallie North
Logisticare Transportation Provider Login
6030 Topsail Rd, Lady Lake, FL 32159 - MLS G5087027 - Coldwell Banker
100X35 Puerto Rico Meaning
Odu Csnbbs
Cbs Sportsline Fantasy Rankings
How Did Laura Get Narally Pregnant
Greg Teaches An Art Class
02488 - Uitvaartcentrum Texel
This Meteorologist Was Wardrobe Shamed, So She Fought Back | Star 101.3 | Marcus & Corey
Mybrownhanky Com
Buhsd Studentvue
Magnifeye Alcon
Hughie Francis Foley
Circle K Wikipedia
‘A Knights Tale’ Turns 20: Secrets Behind Heath Ledger’s Royal Rock Flick
Stuckey Furniture
304-733-7788
Fintechzoommortgagecalculator.live Hours
Captain Phillips Full Movie Free
Conan Exiles Rhino Guide - Conan Fanatics
General Format - Purdue OWL® - Purdue University
Navy Qrs Supervisor Answers
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 6376

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.