How to Find and Delete Directory in Linux [3 Methods]

Finding and deleting a directory is a common task for many Linux users. Sometimes, it becomes necessary either to clear the junk folders or free up the system space. So, if you ever get confused about the finding & removal methods of directories, then this article would be an efficient guide for you. Here, I will explain several methods to find and delete a directory in Linux with every single step to lessen your worries. Let’s explore together.

Key Takeaways

  • Learning to find directories using find and ls commands in Linux.
  • Getting familiar with rmdir, rm -ri & rm -rf commands to delete directories.
  • Getting more ideas by going through the frequently asked questions & their answers regarding directory deletion.

Requirements

  • You must have Ubuntu installed on your PC.
  • You must have root/sudo privileges.
  • You need to back up your important files & folders before starting the deletion process.

Process Flow Chart

[Distro Used Throughout the Tutorial: Ubuntu 22.04.2 LTS]

Flowchart of finding and deleting directories in Linux

Watch 3 Methods to Find and Delete Directory in Linux

3 Methods to Find and Delete Directory in Linux

Before deleting a directory you must be sure if it is empty or not. Because the removal procedures for empty and non-empty directories are quite different. Here, I am going to explain 3 methods of how you can find and delete a directory in Linux.

You can read our Comparative Analysis of Methods to distinguish among these three methods and best pick one for your needs.

Method 01: Using the “find” & “rmdir” Command to Find and Delete Empty Directories in Linux

You may find out that the removal procedure of empty directories is easier than the non-empty ones. Only the find command and the rmdir command are used to find and delete directories. Follow the steps below given accordingly.

Steps to Follow >

➊ At first, open the Ubuntu terminal.

➋ Then, navigate to the directory you want to delete by executing the following command. Here, I want to delete the new_dir directory inside the Desktop.

cd Desktop/new_dir
EXPLANATION
  • cd: Changes the directory.
  • Desktop/new_dir: The folder new_dir that is inside the Desktop.

Now, check if the directory is empty or not by running the command below:

ls -l
EXPLANATION
  • ls: Lists files and directories in the specified directory “new_dir”.
  • -l: Shows a detailed list view.
Navigating to 'new_dir' folderFrom the above image, you can see the directory is empty.

➌ Move on to the following command to find and delete the specific folder you want: 

find /home/nadiba/Desktop -type d -name "new_dir" | xargs rmdir
EXPLANATION
  • find: Finds files and directories.
  • /home/nadiba/Desktop: The directory from where you want to search for the directory you want to remove.
  • -type d: Finds only the directories.
  • -name “new_dir”: Finds only the given directory named “new_dir.
  • |: Passes the output of find command as input to rmdir command.
  • xargs: Builds and executes commands from standard input.
  • rmdir: Removes the empty directory.

Now, write the command below to check if the directory still exists or not:

ls
EXPLANATION
  • ls: Lists files and directories in the specified directory.
find and delete empty directory using 'rmdir' commandFrom the screenshot above, you can see the directory new_dir is removed.

Read More: How to Force Remove Directory in Linux? [Step-by-Step]

Method 02: Using the “find” & “rm -ri” Command to Find and Delete Non-Empty Directories Interactively in Linux

If you want to remove a non-empty directory as like an empty directory using rmdir command, then it will show an error like the one shown in the image below:Error while deleting non-empty directory "red" using: rmdir redYou have already got it that it’s not the correct method, right? So, below I will discuss the removal process of a directory that is non-empty using different commands.

Steps to Follow >

➊ Firstly, open your Ubuntu terminal.

Then, navigate to the directory where your desired directory resides in by executing the following command. I have navigated to the color directory where I can find my desired red directory.

cd Desktop/color
EXPLANATION
  • cd: Changes the directory.
  • Desktop/color: The folder that contains the required red directory.
Navigating to directory named 'color'The directory has been changed to color like the above image.

➌ Now, type the following command to find and delete the specific folder you want to delete from Desktop:

find /home/nadiba/Desktop/color -type d -name "red" -exec rm -ri {} \;
EXPLANATION
  • find: Finds files/ directories.
  • /home/nadiba/Desktop/color: The directory from where you want to search for the directory you want to remove.
  • -type d: Finds only the directories.
  • -name “red”: Finds only the specific directory named “red.
  • exec: Permits command execution on each found item.
  • rm -ri: Removes the directory & all the files in it interactively by asking for permission.
  • {}: Indicates the found directory.
  •  \;: Ends the exec command.

Now, write the ls command to check if the directory has been deleted or not. 

ls
EXPLANATION
  • ls: Lists files and directories in the specified directory.
Finding and deleting non-empty directory 'red' using rm -ri commandFrom the snapshot, you can see that the red directory has been removed.

Warning: If you want to check before deleting, you must use the rm -ri command. This will delete the directory interactively.

Read More: How to Remove a Non-Empty Directory in Linux [2 Methods]

Method 03: Using the “ls” & “rm -rf” Command to Find and Delete Non-Empty Directories in Linux

There is another way of viewing directories using the ls command. It shows the list of files and folders contained in the current directory. Go through the steps below & explore the new method.

Steps to Follow >

➊ Firstly, open your Ubuntu terminal.

➋ Then, navigate to Downloads directory by executing the following command:

cd Downloads
EXPLANATION
  • cd: Changes the directory.
  • Downloads: The folder that contains the required snacks.
Navigating to Downloads by: cd DownloadsFrom the above image, the directory has been changed to Downloads.

➌ Now, type the following command to view all the files & folders in the Downloads directory including the desired one:

ls
EXPLANATION
  • ls: Lists all files and directories.
Viewing snacks directory by: lsIn the above image, you can see the folder snacks which you want to delete.

❹ To delete the directory, now write the following command:

rm -rf snacks
EXPLANATION
  • rm: Removes files & directories.
  • rm -rf: Removes the directory recursively & without asking permission.
  • snacks: Directory name.

This command will directly delete the directory without asking any permission.

❺ Now, press ENTER & check if the snacks directory is still here or not by hitting the ls command.Deleting directly snacks directory by: rm -rf snacks in LinuxThe image shows that there is no longer any directory called snacks.

Caution! It may be risky. So, be careful while using this command. Otherwise, it will ruin all your effort.

Read More: How to Remove a User and Home Directory in Linux [2 Methods]

Comparative Analysis of Methods

Feeling confused about which method to pick? Well, to assist you, I have presented a comparative analysis of the above three methods here marking the pros and cons of each one. Let’s have a look!

Methods Pros Cons
Method 1
  • Very simple syntax.
  • Error probability is low.
  • Cannot delete non-empty directories.
Method 2
  • Safe process.
  • Slower.
Method 3
  • Requires less time.
  • Risky process.

To sum up, it can be said that each method has its own way of functioning in different cases. But as method 2 and method 3 are for the same case, I would recommend you to choose method 2 because you can review all once again before the final deletion which is the safest part. Now, it’s up to you which you are going to pick.

Conclusion

The whole article wraps up that finding and deleting a directory in Linux is a very simple task. All you need is to be conscious before executing any program and prepared for any kind of consequences. So, making a backup of every important content before modification is a wise decision. Well, to conclude, I hope this article will remove all your headaches regarding how to find and delete a directory in Linux.

People Also Ask

How do I delete all files in the current directory in Linux?
First, navigate to the directory from where you want to delete all files. Then, follow the command rm * to remove all files from your current directory.

Why cannot I remove a directory in Linux?
You cannot remove a directory in Linux because you don’t have proper permission to make any modifications in that directory. In this case, you can run the sudo rm -rf  “directory” command which means you are deleting the files with root power.

Can I recover the files deleted with rm command?
The rm command contains no recycle bin and nothing is retrievable as the metadata is cleared nowadays.

How to remove the home directory in Linux?
By using rm –remove/-r command, you can remove the home directory in Linux. But before this, you must have the root/sudo privileges. The right syntax is: sudo rm -/home/user_name.

How does the rm command differ from rmdir command?
rm command removes the directories, including the sub-directories and files, while rmdir command deletes only the empty directories.

Related Articles

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment