DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Arm64 Is No Longer the Edge Case
  • TOP-5 Lightweight Linux Distributions for Container Base Images
  • The Self-Healing Directory: Architecting AI-Driven Security for Active Directory
  • Recent Linux Kernel Features Relevant to System Design

Trending

  • Why Successful Tech Transformation Starts With Clarity
  • Deploying Small Language Models on Sovereign Cloud
  • PWA on iOS, Part 6: Performance, Rendering, and Keyboard
  • Microsoft Foundry Agent Debugging Dashboard with Microsoft Fabric Eventhouse, KQL, Docker and Grafana

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. How to Remove Empty and Non-Empty Directories in Linux [Snippets]

How to Remove Empty and Non-Empty Directories in Linux [Snippets]

Let's look at how to remove a non empty directory in Linux

By 
Johnny Simpson
·
Feb. 09, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
3.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Removing a directory in Linux is harder than you think, and it's not uncommon to get errors that look like this:

folder: Directory not empty
folder: is a directory

The confusion comes because rmdir cannot be used to delete a directory with files or folders inside of it.

Remove a Non-Empty Directory in Linux

To remove an unempty directory in Linux, pass the -r flag to rm. -r means recursive, so it deletes everything in a folder, including the folder itself. Replace 'folder' below with your folder's name or location.

Shell
 
rm -r folder


Remove Multiple Non-Empty Directories in Linux

Simply separate the folder names with a space, and use rm -r:

Shell
 
rm -r folder1 folder2 folder3


Remove an Empty Directory

If your directory is empty, with no folders or files in it, then use rmdir:

Shell
 
rmdir folder



Directory Linux (operating system)

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Arm64 Is No Longer the Edge Case
  • TOP-5 Lightweight Linux Distributions for Container Base Images
  • The Self-Healing Directory: Architecting AI-Driven Security for Active Directory
  • Recent Linux Kernel Features Relevant to System Design

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook