How to Remove a Directory in Linux – Delete a Folder Command (2024)

/ #command line
How to Remove a Directory in Linux – Delete a Folder Command (1)

By Dillion Megida

If you're using a user interface, you can right-click on a directory and select "Delete" or "Move to Bin". But how do you do this on the terminal? I'll explain that in this article.

How to Remove a Directory in Linux

There are two ways to remove directories in Linux: the rm and rmdir commands.

The TL;DR of both commands is that rm deletes directories that may contain content such as files and subdirectories, while rmdir ONLY deletes empty directories.

Also, both commands delete directories permanently (rather than moving them to the trash), so be careful when using them.

Let's look at both commands in more detail.

How to Use the Linux rm command

You use the rm command to delete files and directories in Linux. For directories, this command can be used to delete a directory entirely – that is, it deletes a directory and all files and subdirectories within the directory.

Here's the syntax of this command:

rm [options] [files and/or directories]

To remove a file, say test.txt, you can use the command without options like this:

rm test.txt

For directories, you have to provide some flag options.

How to delete a folder with contents

For a directory with contents, you have to provide the -r flag. Without using this flag like this:

rm test

You will get this error: rm: test: is a directory

The -r flag informs the rm command to recursively delete the contents of a directory (whether it's files or subdirectories). So, you can delete a directory like this:

rm -r test

How to delete an empty folder

For an empty folder, you can still provide the -r flag, but the dedicated -d flag applies to this case. Without this flag, you will get the same error rm: [folder]: is a directory.

To delete an empty directory, you can use this command:

rm -d test

It is recommended to use the -d flag for empty directory cases instead of the -r flag because the -d flag ensures that a directory is empty.

If it is not empty, you will get the error rm: test: Directory not empty. So, to be sure you are performing the proper empty directory operation, use the -d flag.

How to Use the Linux rmdir Command

The rmdir command is specifically used to delete empty directories. The syntax is:

rmdir [folders]

It is the equivalent of the rm command with the -d flag: rm -d.

When you use rmdir on a non-empty directory, you get this error: rmdir: [folder]: Directory not empty.

To delete an empty directory, use this command without options:

rmdir test

The rmdir command also has the -p flag, which allows you to delete a directory along with its parent in the tree. For example, if you have this file structure:

> Test---> Test22

In this case, Test is a directory that has the Test2 subdirectory. If you delete the Test2 directory, Test becomes an empty directory. So instead of doing:

rmdir Test/Test2 Test# deleting Test2 and then Test

You can use the -p flag like this:

rmdir -p Test/Test2

This command will delete Test2 and afterward delete Test, the parent in the tree. But this command will throw an error if either directory is not empty.

How to Delete Directories that Match a Pattern in Linux

You can also use rm and rmdir with glob patterns. Globbing is similar to Regex, but the former is used for matching file names in the terminal.

For example, if you want to delete the directories test1, test2, and test3, instead of running:

rm -r test1 test2 test3# or if they are emptyrmdir test1 test2 test3

You can use a wildcard glob pattern like this:

rm -r test*# or if they are emptyrmdir test*

The asterisk * matches any mixture of characters after the "test" word. You can also apply other glob patterns. Learn more in the globbing documentation

Wrap Up

Now you know how to delete directories in Linux from the command line. You learned about the rm and rmdir commands and when to use each.

Happy coding!

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

How to Remove a Directory in Linux – Delete a Folder Command (2024)
Top Articles
Four-leaf clover: Why are they considered a symbol of good luck?
The enterprise of the future is composable. Does your enterprise have a composable platform strategy?
Pet For Sale Craigslist
Ghosted Imdb Parents Guide
Black Gelato Strain Allbud
Byrn Funeral Home Mayfield Kentucky Obituaries
Seething Storm 5E
Toyota gebraucht kaufen in tacoma_ - AutoScout24
Truist Drive Through Hours
Obituary | Shawn Alexander | Russell Funeral Home, Inc.
Blog:Vyond-styled rants -- List of nicknames (blog edition) (TouhouWonder version)
Breakroom Bw
Sams Early Hours
Wildflower1967
Insidekp.kp.org Hrconnect
Games Like Mythic Manor
Ms Rabbit 305
Poe Str Stacking
north jersey garage & moving sales - craigslist
Purdue 247 Football
Ac-15 Gungeon
Plaza Bonita Sycuan Bus Schedule
Yayo - RimWorld Wiki
Guinness World Record For Longest Imessage
Helpers Needed At Once Bug Fables
Christmas Days Away
Workboy Kennel
How to Draw a Bubble Letter M in 5 Easy Steps
Sitting Human Silhouette Demonologist
Hattie Bartons Brownie Recipe
Jefferson Parish Dump Wall Blvd
The Vélodrome d'Hiver (Vél d'Hiv) Roundup
Stafford Rotoworld
Philadelphia Inquirer Obituaries This Week
One Main Branch Locator
Joey Gentile Lpsg
Google Flights Orlando
Omaha Steaks Lava Cake Microwave Instructions
Craigslist Boats Dallas
Acts 16 Nkjv
Tfn Powerschool
Atu Bookstore Ozark
M&T Bank
844 386 9815
Brown launches digital hub to expand community, career exploration for students, alumni
Ts In Baton Rouge
Minterns German Shepherds
Mlb Hitting Streak Record Holder Crossword Clue
Puss In Boots: The Last Wish Showtimes Near Valdosta Cinemas
Yoshidakins
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 6216

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.