How to Remove Directory in Linux - GeeksforGeeks (2024)

Last Updated : 19 Apr, 2024

Improve

In Linux, a Directory is a container for files and other directories, forming a hierarchical structure. so, before deep diving into the directory, let’s learn about the Linux Directory. A directory is a structure that plays an important role in the file system, offering users a systematic way to navigate and structure their data. In the Linux System Directories are essential for maintaining order and providing a clear path for accessing files. Directories enable users to categorize related files, making it easier to locate and manage them. The directory structure follows a tree-like hierarchy, with the root directory at the top, branching into subdirectories, and further dividing into files. Users can traverse through directories using commands like “cd” (change directory) in the Linux terminal.

How to Remove Directory in Linux?

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

Method 1: Using the rmdir Command to Remove Directory

In this section, we will explore the detailed steps to remove the directory in Linux by using the rmdir inbuilt command.

Syntax:

rmdir [options] [directory name]

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: Firstly, open your Linux system and open the terminal. You will see an interface like this, as shown in the image below.

hostnamectl

How to Remove Directory in Linux - GeeksforGeeks (1)

Opening Terminal and Running hostnamectl Command

Step 2: After that Before establishing a new directory, use the pwd command to determine where you are in the filesystem.

pwd

Step 3: Now, before going to delete any directory on the Linux system, we first have to create a new directory to perform the task using the below command. Check its options and available parameters.

mkdir --help

How to Remove Directory in Linux - GeeksforGeeks (3)

Viewing Help Section of mkdir Command

Step 4: Now we have to use the above command and create the one directory name as ‘GeeksforGeeks‘ then we see how to delete it.

mkdir GeeksforGeeks

How to Remove Directory in Linux - GeeksforGeeks (4)

Creating a New Directory Named GeeksforGeeks

Step 5: To check if the directory GeeksforGeeks was created on your system or not, use the ls command and check it.

ls

How to Remove Directory in Linux - GeeksforGeeks (5)

Listing files and directories of /home/kali

We will navigate to the GeeksforGeeks directory and view its contents.

cd GeeksforGeeks
ls

How to Remove Directory in Linux - GeeksforGeeks (6)

Navigating to GeeksforGeeks directory

Step 6: Remove the Directory to remove the directory and its contents, use the rmdir command.

rmdir GeeksforGeeks

How to Remove Directory in Linux - GeeksforGeeks (7)

Removing GeeksforGeeks directory

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]

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: Firstly, open your Linux system and open the terminal. You will see an interface like this, as shown in the image below.

Step 2: Navigate to the Parent Directory Use the cd command to navigate to the parent directory containing the target directory.

cd /home/kali/Documents/

How to Remove Directory in Linux - GeeksforGeeks (8)

Navigating to the Parent Directory

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

rm -r GeeksforGeeks

How to Remove Directory in Linux - GeeksforGeeks (9)

Removing the GeeksforGeeks directory in a Recursive manner

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

rm -ri directory_name

How to Remove Directory in Linux - GeeksforGeeks (10)

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.

How to Remove Directory in Linux – FAQs

How do I remove a directory in Linux using the command line?

To remove a directory in Linux, you can use the rmdir or rm command. The rmdir command is specifically designed for removing empty directories, while the rm command is more versatile and can delete directories and their contents.

Can I remove a directory with confirmation prompts to avoid accidental deletions?

Yes, you can add the -i option to the rm command to prompt for confirmation before removing each file and directory. This helps prevent accidental deletions.

How can I forcefully remove a directory without confirmation prompts?

To forcefully remove a directory and its contents without any confirmation prompts, you can use the -f option with the rm command.



S

sourabhsahu22

Improve

Next Article

How to remove a directory in R?

Please Login to comment...

How to Remove Directory in Linux - GeeksforGeeks (2024)

References

Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6319

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.