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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • 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
  • Scaling Identity Governance Without Connectors: The LDAP Directory IGA Integration Pattern

Trending

  • Stop Running Two Data Systems for One Agent Query
  • Designing API-First EMR Architectures in .NET: Enabling Modular Growth in Compliance-Driven Systems
  • A Walk-Through of the DZone Article Editor
  • How to Write for DZone Publications: Trend Reports and Refcards
  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 user avatar
Johnny Simpson
·
Feb. 09, 22 · Code Snippet
Likes (5)
Comment
Save
Tweet
Share
2.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • 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
  • Scaling Identity Governance Without Connectors: The LDAP Directory IGA Integration Pattern

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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