Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
How to Remove Directory in Linux
Next article icon

How to Remove Directory in Linux

Last Updated : 04 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Linux, directories are used to organize files and other directories into a hierarchical structure. Just like folders in Windows, directories in Linux can contain files, other directories, and links. Removing directories in Linux is a common task that you might need to perform as you manage files and organize your file system.

When you want to remove a directory in Linux, you need to consider whether it is empty or contains files and other directories.

To delete a directory in Linux, there are two commands you can use: rmdir to delete empty directories and rm -r for removing directories with files or subdirectories.

Table of Content

  • Method 1: Using the 'rmdir' Command to Remove Directory
  • Method 2: Using the rm Command to Remove Directory

Method 1: Use 'rmdir' Command - Removing an Empty Directory

To remove an empty directory in Linux, you can use the rmdir command. This command is straightforward and safe because it only allows you to delete directories that are empty, preventing accidental deletion of directories with content.

Syntax:

rmdir [options] [directory name]

Common Options for rmdir:

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.

Step 1: Check Your Current Directory

Firstly, open your Linux terminal. Now, before deleting the directory, use the pwd command to determine where you are in the filesystem:

pwd
Screenshot-from-2024-12-05-11-40-03

Step 2: Remove the Directory

Now to delete an exsisting empty directory, use the rmdir command:

rmdir gfg
Screenshot-from-2024-12-05-11-47-59

Step 3: Confirm the directory was deleted (Optional)

Again write ls command to be assured that the directory was deleted:

ls
Screenshot-from-2024-12-05-11-53-10

gfg directory should no longer appear.

Method 2: Using the rm Command to Remove Directory

The rm command in Linux is a powerful tool for deleting files and directories. When it comes to removing a directory, the '-r' (recursive) option is crucial. This ensures that the directory and its contents are deleted.

Syntax:

rm [options] [file or directory name]

Common Options for rm:

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.

Step 1: Navigate to Parent Directory

Open your linux terminal and navigate to the Parent Directory using the cd command:

cd /home/kali/Documents/
Navigating to the Parent Directory
Navigating to the Parent Directory

Step 2: Remove the Directory

Remove the Directory To remove the directory and its contents, use the rm command with the -r (recursive) option.

rm -r GeeksforGeeks
Removing the GeeksforGeeks directory in a Recursive manner
Removing the GeeksforGeeks directory in a Recursive manner

Step 3: Optional Confirmation

Confirm Removal If you want to confirm each deletion, you can add the '-i' option to prompt for confirmation.

rm -ri directory_name
Confirming Removal of Directory
Confirming Removal of Directory

So using the rmdir command, we can delete the directory of our system. As you can see in the image, initially, when we created the directory, it was in our system. However, after the deletion, it was not shown in our file directories, and the file was successfully deleted.

Conclusion

In conclusion, removing a directory in Linux is a straightforward process that involves utilizing the 'rmdir' or 'rm' command, depending on whether the directory is empty or contains files. Linux users can efficiently manage their file system by employing these commands to delete unwanted directories and streamline their directory structure.

It is crucial to exercise caution while using the rm command, ensuring the correct directory is specified to prevent accidental data loss. Regularly cleaning up directories enhances system organization and optimizes storage space on Linux systems.


Next Article
How to Remove Directory in Linux

S

sourabhsahu22
Improve
Article Tags :
  • Linux-Unix
  • Geeks Premier League
  • linux
  • Geeks Premier League 2023

Similar Reads

    How to remove a directory in R?
    In this article, we will discuss how to remove a directory using R programming language. To remove a directory in R we use unlink(). This function deletes the named directory. Syntax: unlink(directory-name, recursive = BOOLEAN) Parameter: directory-name: a character vector with the names of the dire
    1 min read
    How to Manage Directories in Linux?
    Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories
    6 min read
    How to Copy a Directory in Linux
    There is this famous quote for Linux by Jamie Zawinski that goes like this "Linux is only free if your time has no value." Certainly, that's true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about
    5 min read
    How to Rename a Folder in Linux
    Renaming a folder in Linux is possible with Graphical User Interface (GUI) file managers as well as with powerful command-line utilities such as mv, rename, find, and rsync. Be it a novice utilizing Ubuntu, Debian, CentOS, Fedora, or Kali Linux or an expert dealing with bulk renaming in the terminal
    12 min read
    Remove a Symbolic link to a Directory in Linux
    Symbolic links (symlinks) are very important when managing files under Linux since they allow users to make shortcuts of directories and files without copying data. While they help improve the system organization and efficiency, sometimes you might be required to remove a symlink from a directory to
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences