Open In App

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.

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

Similar Reads