Knowledgebase

How To Remove Directory in Linux With Command?

Every Linux user should know how to manage directories. Removing directories is a common task, whether cleaning up old files or organising your workspace. It might seem daunting for new users, but it’s quite straightforward once you understand the commands.

In this article, we will explore how to remove directories using two primary commands: rmdir and rm.

This guide will show you how to remove directories in Linux using two primary commands: rmdir and rm. Don’t worry if you’re a beginner; we will walk you through step by step!

Let’s get started!

Understanding the Commands

Before we dive into the steps, it’s important to understand the commands you must use to remove the Linux directory.

rmdir: This command removes empty directories. It will not work if your directory contains files or other directories.

rm: This command is more powerful and can remove directories and their contents. Please note that the -r (recursive) option is required to delete non-empty directories.


Also Read: How to Log into SSH using Password Authentication?


What You Need to Know Before Removing a Directory

Before we dive in, here are a few things to keep in mind:

Permissions: It is required to have permission to delete a directory. If you don’t own the directory, you might need sudo to proceed.

Empty vs. Non-Empty Directories: As discussed above, Linux treats empty and non-empty directories differently when it comes to deletion. Both have different commands for the process. 


Also Read: How to Change Server Time in Linux?


Steps For Removing an Empty Directory

As mentioned above, to remove an empty directory, use the rmdir command.

Syntax:

rmdir <directory_name>

Example:

Let’s say you have an empty directory named test_folder.

➔ Open your terminal.

➔ Navigate to the parent directory where test_folder is located using cd:

cd /path/to/parent_directory

➔ Before removing it, you need to ensure that the directory is indeed empty by listing its contents:

Ls test_folder

If the directory contains files or subdirectories, they will be listed. If it’s empty, the command will return nothing.

➔ Use the rmdir command after ensuring that the directory is empty.

rmdir test_folder

If the directory is empty, it will be removed without any issues.

In case you didn’t check before whether the file contains content, you will get an error message like this:

rmdir: failed to remove ‘test_folder’: Directory not empty

## Command to Remove a Non-Empty Directory

To remove a directory containing files or subdirectories, use the `rm` command with the `-r` (recursive) option.

### Syntax:
“`bash
rm -r <directory_name>

Also Read: How To Access Linux Server Using PuTTY SSH Terminal?


Steps to Remove a Non-Empty Directory

If you need to delete a directory that contains files or other directories, follow these steps:

Example:

Suppose test_folder contains files or subdirectories.

➔ Navigate to the parent directory by running the following command.

cd /path/to/parent_directory

➔ Use the rm command with the -r option:

rm -r test_folder

This will remove the directory along with all its contents.

Be Careful:

➔ Always double-check the directory name before running the rm -r command.

➔ If you want a confirmation prompt for each file being deleted, use the -i option:

rm -ri test_folder

Also Read: Install & Configure a CSF Firewall in a CenTOS Server


Steps to Remove a Directory with sudo

As discussed above, If you don’t have permission to delete a directory, you can use sudo to execute the command with administrative rights.

Syntax:

sudo rm -r <directory_name>

Example:

➔ Let’s say you want to delete a directory named restricted_folder that requires administrative rights. To do so, run the following command.

sudo rm -r restricted_folder

You will be prompted to enter your password. After entering it, the directory will be removed.

Conclusion

Removing directories in Linux is straightforward once you understand the commands. Whether it’s an empty directory or one filled with files, you now have the commands to clean up your Linux environment confidently.

By following these steps, even new users can confidently navigate their file systems and perform deletions safely. Practice these commands and remember to handle deletions carefully to avoid accidental data loss.