Remove Directory in Linux PROPERLY & SAFELY


Linux

When working with Directories (folders) on Linux-based systems, you will be required to delete some once in a while. There are two main ways which you can use to remove directories on your Linux system:

  1. Graphical User Interface (GUI)
  2. Command-Line (Terminal)

 

Remove directory in Linux via GUI Console

The GUI method is relatively straightforward if you have a Desktop environment installed on your system. Some of the most common Desktop Environments available for Linux systems include GNOME, Xfce, MATE, KDE, and many more. To remove a directory via the Graphical method, all you need is to select the folder and press the delete button. Alternatively, you can right-click on it and select the option "Move to Trash," as shown in the image below.

Remove Directory in Linux PROPERLY & SAFELY

 

What You Need To Know Before Removing Directories

The GUI method of deleting directories brings a new terminology - "Trash." If you are familiar with Windows operating system, Trash is similar to Recycle Bin. Therefore, when you remove any files or folders via the Graphical User Interface on Linux, they are automatically moved to Trash to be recovered later or permanently deleted.

Unfortunately, that's not the case with the Command-Line (Terminal) method. If you remove a directory using the Terminal, it is permanently deleted and cannot be restored (Unless you use data recovery tools). Luckily, Linux uses file permission to manage access to these directories. For example, most directories in the root folder will ask for root privileges to remove them; otherwise, you will get the error "operation not permitted" or "remove write-protected directory."

Tip: If you wish to permanently delete a directory or file on Linux via the GUI method, select the directory -> hold the Shift key and press delete.

 

Remove Directories from the Command-Line

The Linux terminal is the most powerful utility you can utilize on your distribution. This post will look at three commands we can use to delete/ remove directories on our system.

  1. rmdir
  2. rm
  3. find

Let's dive in.

 

Remove Directories With the rmdir Command

The rmdir utility is used to delete only empty directories without checking whether the directory is empty or not. If you try deleting a directory that's not empty, you will get the error "rmdir: failed to remove 'testDirectory/': Directory not empty." This command uses the syntax below:

rmdir <directory name>
e.g
rmdir testDirectory

In the image below, we created an empty directory called "testDirectory" and used the rmdir command to delete it.

Remove Directory in Linux PROPERLY & SAFELY

Now, let's try creating a new directory -> add a new file inside, then try to delete it using the rmdir command.

Remove Directory in Linux PROPERLY & SAFELY

From the image above, you can see that rmdir could not delete the directory since we had created a new file, "textOne," using the touch command. To remove a directory and its contents, we will need o see the rm command below.

 

Remove Directories With the rm Command

rm is another powerful command that you can use to remove both files and directories on your Linux system. However, unlike rmdir, the rm command can remove empty and non-empty directories. Additionally, it uses parameters that you can use to specify how you want to delete the directories. You can view these parameters by running the rm --help command.

Executing the command rm alone on an empty or non-empty directory will not delete the directory. Instead, you will get the error "rm: cannot remove 'test': Is a directory," as shown in the image below.

Remove Directory in Linux PROPERLY & SAFELY

You will need to use various parameters for every action you wish to execute with the rm command. Let's discuss some of these parameters.

To delete an empty directory, use the -d parameter shown in the syntax below.

rm -d <directory name>
e.g
rm -d testDirectory

To delete a non-empty directory and its contents, use the -r (recursive) parameter.

rm -r <directory name>
e.g
rm -r testDirectory

To delete a write-protected directory, you will require root privileges. Therefore, you need to start your command with the name sudo as shown below.

sudo rm -r testDirectory

To remove multiple directories all at once, execute the rm command followed by the names of all the directories you wish to delete, as shown below.

rm -r testDirectory1 testDirectory2 testDirectory3

You can also use regular expressions together with the rm command. For example, to remove all directories that end with 'bak,' you can use the syntax below.

rm -r *bak

However, that can be risky since you can remove directories you did not intend to delete.

 

Find and delete directories and sub-directories

find is a popular Linux command used to search for files and directories based upon a given regular expression. However, you can use find command to delete directories by following a given pattern. Let's take a look at the command below.

find . -type d -name '*_bak' -exec rm -r {} +

Basically, the command above searches and deletes any directories that end with the suffix "_bak." Let's go through every argument in detail.

  • find . : This command tells find to search for files in the current working directory
  • -type d: This parameter tells find we are only searching for directories.
  • -name '*_bak': This parameter restricts the search to only directories ending with "_bak."
  • -exec: This command executes another command with its arguments. In our case, it's the rm -r command.
  • {} +: This parameter appends all the found directories to the rm -r command for execution.

 

Conclusion

This post has given you four methods (Command-line and GUI) that you can use to remove directories on your system. Deleting directories is quite simple; however, you should always be cautious since you can lose important data, especially with the Terminal commands that permanently delete your data. If you have any questions or feedback, please feel free to leave a comment.

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment