FUNDAMENTALS A Complete Guide for Beginners
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]
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.
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
- 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
- ls: Lists files and directories in the specified directory “new_dir”.
- -l: Shows a detailed list view.
➌ 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
- 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
- ls: Lists files and directories in the specified directory.
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:You 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
- cd: Changes the directory.
- Desktop/color: The folder that contains the required red directory.
➌ 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 {} \;
- 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
- ls: Lists files and directories in the specified directory.
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
- cd: Changes the directory.
- Downloads: The folder that contains the required snacks.
➌ Now, type the following command to view all the files & folders in the Downloads directory including the desired one:
ls
- ls: Lists all files and directories.
❹ To delete the directory, now write the following command:
rm -rf snacks
- 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.The image shows that there is no longer any directory called snacks.
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 |
|
|
Method 2 |
|
|
Method 3 |
|
|
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
Related Articles
- Remove All Files from Current Directory in Linux [2 Methods]
- How to Undelete Folder in Ubuntu? [With Solutions]
- 3 Ways to Remove Write Protected Regular File Using the rm command
- 2 Cases of Recursive Remove of Directory in Linux
- 3 Ways to Remove All Files in a Linux Directory
- Remove All Files in a Directory with the Prompt in Linux