DZone
Open Source Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Open Source Zone > 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

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Feb. 09, 22 · Open Source Zone · Code Snippet
Like (5)
Save
Tweet
1.74K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Chopping the Monolith: The Demo
  • Instancio: Test Data Generator for Java (Part 2)
  • Modern Application Security Requires Defense in Depth
  • Dynamically Provisioning Persistent Volumes with Kubernetes

Comments

Open Source Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo