Linux find directories and remove

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/sudoprivileges.
  • 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 : Changes the directory.
  • Desktop/new_dir : The folder new_dir that is inside the Desktop.
Читайте также:  Драйвер видеокарты linux ubuntu

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

  • ls : Lists files and directories in the specified directory “ new_dir ”.
  • -l : Shows a detailed list view.

Navigating to

From 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
  • 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:

find and delete empty directory using

From the screenshot above, you can see the directory new_dir is removed.

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

Error while deleting non-empty directory

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 : Changes the directory.
  • Desktop/color : The folder that contains the required red directory.

Navigating to directory named

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 <> \;
  • find : Finds files/ 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 “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.

Finding and deleting non-empty directory

From the snapshot, you can see that the red directory has been removed.

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

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.

Читайте также:  Линуксы для arm процессоров

Steps to Follow >

➊ Firstly, open your Ubuntu terminal.

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

  • cd: Changes the directory.
  • Downloads: The folder that contains the required snacks.

Navigating to Downloads by: cd Downloads

From 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:

Viewing snacks directory by: ls

In the above image, you can see the folder snacks which you want to delete.

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

  • 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.

Deleting directly snacks directory by: rm -rf snacks in Linux

❺ 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.

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

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!

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.

Читайте также:  All important linux commands

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

  • 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

Источник

How to Search and Remove Directories Recursively on Linux

In one of our previous articles, we explained how to find out top directories and files consuming the most disk space on file system in Linux. If you notice that such directories no longer contain important files and subdirectories (such as old backups, downloads etc..), then you can delete them to free up space on your disk.

This short tutorial describes how to find and delete directories recursively in the Linux file system.

To achieve the above purpose, you can employ the find command together with rm command using the syntax below. Here, the + sign at the end enables multiple directories to be read simultaneously.

$ find /start/search/from/this/dir -name "dirname-to-delete" -type d -exec /bin/rm -rf <> +

Attention: You must use rm command carefully because it is one of the most dangerous commands to use in Linux: you may accidentally delete critical system directories, thus resulting to system failure.

In the example below, we will search for a directory called files_2008 and delete it recursively:

$ $find ~/Downloads/software -name "files_2008" -type d -exec /bin/rm -rf <> +

You can also use find and xargs; in the following syntax, -print0 action enables printing of the full directory path on the standard output, followed by a null character:

$ find /start/search/from/this/dir -name "dirname-to-delete" -type d -print0 | xargs -0 /bin/rm -rf "<>"

Using the same example above, we have:

$ find ~/Downloads/software -name "files_2008" -type d -print0 | xargs -0 /bin/rm -rf "<>"

Last but not least, if you are concerned about the security of your data, then you may want to learn 3 ways of permanently and securely deleting ‘Files and Directories’ in Linux.

Do not forget to read more useful articles about file and directory management in Linux:

In this article, we showed you how to find and remove directories recursively on Linux. If you have any question or extra ideas you want to add to this topic, use the comment section below.

Источник

Оцените статью
Adblock
detector