Categories
Interior

How to remove multiple subdirectories with one linux command

Lori Kaufman is a technology expert with 25 years of experience. She’s been a senior technical writer, worked as a programmer, and has even run her own multi-location business. Read more.

How to remove multiple subdirectories with one linux command

If you want to remove several subdirectories within another directory using the command line in Linux, generally you have to use the rm command several times. However, there is a faster way to do this.

Let’s say we have a directory called htg with five subdirectories within it and we want to delete three of them. In a normal situation, we’d use the rm command three times.

How to remove multiple subdirectories with one linux command

However, we can make this process even shorter by combining the three rm commands into one. Here’s how.

To remove the three subdirectories you only need to type the following command at the prompt and press Enter (obviously, change the directory names to what you want to remove).

The words in the brackets are part of the “brace expansion list”. Each of the items in the brace expansion list is appended separately to the preceding path (

/Documents/htg/). For example, the above command is expanded into

/Documents/htg/notes, the three subdirectories under the htg directory that we want to remove. As you can see in the screenshot below, those three subdirectories were removed.

How to remove multiple subdirectories with one linux command

The -r flag is required when using the rm command to remove a directory rather than a file. If you leave the -r flag out of the above command, you will get an error saying that the directories cannot be removed.

How to remove multiple subdirectories with one linux command

If all of the subdirectories you want to remove are empty, you can use the rmdir command, as shown below.

If it turns out that any of the subdirectories are not empty, an error will display saying that the removal failed and the subdirectory in question and its subdirectories are not removed. However, any empty subdirectories are removed.

How to remove multiple subdirectories with one linux command

Be very careful with the rm command. Using it the wrong way can delete all the files on your hard drive.

Lori Kaufman is a technology expert with 25 years of experience. She’s been a senior technical writer, worked as a programmer, and has even run her own multi-location business. Read more.

How to remove multiple subdirectories with one linux command

If you want to create a directory containing several subdirectories, or a directory tree, using the command line in Linux, generally you have to use the mkdir command several times. However, there is a faster way to do this.

Let’s say we’ve created a directory called htg, and want to create four subdirectories within it. In a normal situation, we’d use the mkdir command to create the htg directory. Then, we’d need the cd command to change to the new htg directory and, finally, we we’d use the mkdir command again four times to create the four subdirectories.

How to remove multiple subdirectories with one linux command

This can all be combined into one command, and we’ll show you how.

To create a new directory with multiple subdirectories you only need to type the following command at the prompt and press Enter (obviously, change the directory names to what you want).

The -p flag tells the mkdir command to create the main directory first if it doesn’t already exist (htg, in our case). The words in the brackets are part of the “brace expansion list”. Each of the items in the brace expansion list is appended separately to the preceding path (htg/).

How to remove multiple subdirectories with one linux command

For example, the above command is expanded into htg/articles, htg/images, htg/note, htg/done, all four of the subdirectories being created under the htg directory. As you can see in the screenshot below, it worked.

How to remove multiple subdirectories with one linux command

You can also use a brace expansion list in the mkdir command if you’re creating subdirectories in a directory that already exists, as shown below. In this example, the htg directory already exists so the subdirectories are simply added under that directory.

How to remove multiple subdirectories with one linux command

You can also nest brace expansion lists in the mkdir command. For example, in the articles subdirectory under the htg directory, we want to create two subdirectories called new and rewritten. So, we type the following command at the prompt and press Enter.

You can also use the full path if you want, as I’ve done in the example below:

The four subdirectories are created under the htg directory and then the two subdirectories, new and rewrites, are created under the articles subdirectory.

How to remove multiple subdirectories with one linux command

It’s that easy. You can also combine the mkdir command with the cd command to make a directory and change to it with one command.

Home » SysAdmin » How to Remove a Directory in Linux

Removing a directory in Linux is a pretty simple task if you are using the GUI. However, if you don’t have access to the GUI, you can also remove directories using terminal commands.

In this tutorial, we will show you how to remove a directory in Linux via commands in the terminal window or command line.

How to remove multiple subdirectories with one linux command

  • A system running a Linux distribution.
  • An account with sudo privileges.
  • Access to the terminal window or command line.

Note: For other Linux directory management articles, see How to Move Directories in Linux and How to Rename a Directory in Linux.

How to Remove a Directory in Linux?

There are two Linux commands you can use to remove a directory from the terminal window or command line:

  • The rm command removes complete directories, including subdirectories and files.
  • The rmdir command removes empty directories.

It is important to note that the rm and rmdir commands permanently remove directories without moving them to the Trash directory. This means that you cannot restore a directory removed using these commands.

Note: Even though rm and rmdir permanently remove files and directories, users with enough skill and time still have a chance of restoring some of the removed files. If you want to learn more about removing files permanently, have a look at our shred command tutorial.

rm Command

The rm command in Linux removes files and directories.

It uses the following syntax:

Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces.

The different rm command options include:

  • – f : Forces the removal of all files or directories.
  • -i : Prompts for confirmation before removing.
  • -I : Prompts once before removing more than three files or when removing recursively.
  • -r : Removes directories and their content recursively.
  • -d : Removes empty directories.
  • -v : Provides a verbose output.
  • –help : Displays the help text.
  • –version : Displays the command version.

Trying to use the rm command without any options to remove a directory results in an error message:

If you want to remove an empty directory, add the -d flag to the rm command:

Note: If you want to remove a directory whose name starts with a hyphen (), use the rm — [directory name] or rm ./[directory name] syntax.

The example below shows that the rm command with the – d flag removes the Example directory:

How to remove multiple subdirectories with one linux command

Use the -r flag to delete a directory that contains subdirectories and files.

The image below shows the tree hierarchy of the Example directory, which contains Dir1 and Dir2 subdirectories, with multiple text files in each:

How to remove multiple subdirectories with one linux command

Using the -r flag removes the entire directory, including subdirectories and files, while the -v flag lists each step of the process as the output:

How to remove multiple subdirectories with one linux command

The -i option displays a prompt asking you to confirm directory removal. Type Y and press Enter to confirm.

rmdir Command

The Linux rmdir command removes empty directories only. The command uses the following syntax:

The rmdir command includes the following options:

  • –ignore-fail-on-non-empty : Doesn’t show an error message when trying to remove a non-empty directory.
  • -p : Removes the directory along with its parent in the hierarchy.
  • -v : Provides a verbose output.
  • –help : Displays help text.
  • –version : Displays the command version.

Using the rmdir command on a non-empty directory produces an error:

In this case, the Example directory contains the Test subdirectory:

To remove these directories using the rmdir command, add them in reverse order of hierarchy. Using the -v option lists each step of the process as the output:

A simpler method of doing this is to use the -p option with the subdirectory’s name. This removes both the subdirectory and its hierarchical parent:

The rmdir command allows you to remove multiple directories with similar names using wildcards. For instance, if you want to remove directories named Example1, Example2, and Example3:

After reading this tutorial, you should be able to remove directories in Linux using commands in the terminal window or command line.

To learn more about other commands in Linux, check out our Linux commands cheat sheet.

This question is kind of a phase II to the first question I posted at here

I have a directory that contains a bunch of sub-directories, .zip files, and other random files not contained within a sub-directory.

I’d like a command line script to remove all sub-directories from within the parent directory, but keep all zip files and loose files that don’t belong to any sub-directories. All of the sub-directories have content, so I believe I’d need to force their deletion with the -f command.

So basically, a command that looks inside the parent directory (or the current directory), deletes all folders from within it, but keeps all other content and files that are not a folder or contained within a folder.

I understand that deleting items from the command line requires special care, but I have already taken all necessary precautions to back up remotely.

6 Answers 6

In BASH you can use the trailing slash (I think it should work in any POSIX shell):

Note the — which separates options from arguments and allows one to remove entries starting with a hyphen – otherwise after expansion by the shell the entry name would be interpreted as an option by rm (the same holds for many other command line utilities).

Add the -f option if you don’t want to be prompted for confirmation when deleting non-writeable files.

Note that by default, hidden directories (those whose name starts with . ) will be left alone.

An important caveat: the expansion of */ will also include symlinks that eventually resolve to files of type directory. And depending on the rm implementation, rm -R — thelink/ will either just delete the symlink, or (in most of them) delete the content of the linked directory recursively but not that directory itself nor the symlink.

If using zsh , a better approach would be to use a glob qualifier to select files of type directory only:

to include symlinks to directories (but because, this time, the expansion doesn’t have trailing / s, it’s the symlink only that is removed with all rm implementations).

With bash , AT&T ksh , yash or zsh you can do:

to strip the trailing / .

How to remove multiple subdirectories with one linux command

In addition to the wildcard way, you can also use find (at least GNU find) to do this:

As with other find lines, you can run the first part ( find -mindepth 1 -maxdepth 1 -type d ) to see a list of directories that will be removed.

Portably (POSIXly), you’d use the -exec cmd <> + syntax instead of -print0 | xargs -r0 , and -prune do limit the depth:

(and remove the echo to actually do it).

A safer option (here also assuming GNU mv ) is to do something like this:

any of which will move all the stuff into ../to-rm instead of deleting it. You can verify it did what you wanted, them rm -Rf that directory at your leisure.

How to remove multiple subdirectories with one linux command

You might want to create a script for some of these suggestions, especially rm -R — */ , and keep it in your /usr/local/bin folder; or create an alias in your

/.bashrc file. Since it’s so easy to mistype a command and break your system — even a single letter and/or the order of letters can result in catastrophic consequences — this would serve as a somewhat more robust solution than having to type the different options and arguments each time you want to accomplish this task.

Also, you may want to include the -i or –interactive=once or -I or –interactive=always option to your script/command which will serve as another tool to avoid the unintended deletions.

Furthermore, something like derobert suggested would be best; just copy/paste the script into a file/terminal-editor and adjust it to your specific needs, and the files/directories will be moved into a single directory (the contents of which you can check/verify) that you can simply remove by issuing the rm -rf command.

Another option is to use a GUI-application, such as your file manager, and simply select all applicable files/folders you want to delete. Check your distribution’s manual-pages if you don’t have permissions.

Finally, if the folders are empty — essentially simple filenames — you can use the rmdir command to delete them. It doesn’t work for everything you may want to delete, but it will come in handy at times when you want to do some “house-cleaning”. **You may want try the -p –ignore-fail-on-non-empty option, which will allow you to delete certain sub-directories as well as their empty “parents”–the directories in which they reside.

How to delete a file in Linux? How to delete a directory in Linux? Let’s see how to do both of these tasks with one magical command called rm.

How to delete files in Linux

Let me show you various cases of removing files.

1. Delete a single file

If you want to remove a single file, simply use the rm command with the file name. You may need to add the path if the file is not in your current directory.

If the file is write protected i.e. you don’t have write permission to the file, you’ll be asked to confirm the deletion of the write-protected file.

You can type yes or y and press enter key to confirm the deletion. Read this article to know more about Linux file permissions.

2. Force delete a file

If you want to remove files without any prompts (like the one you saw above), you can use the force removal option -f.

3. Remove multiple files

To remove multiple files at once, you can provide all the filenames.

You can also use wildcard (*) and regex instead of providing all the files individually to the rm command. For example, if you want to remove all the files ending in .hpp in the current directory, you can use rm command in the following way:

4. Remove files interactively

Of course, removing all the matching files at once could be a risky business. This is why rm command has the interactive mode. You can use the interactive mode with the option -i.

It will ask for confirmation for each of the file. You can enter y to delete the file and n for skipping the deletion.

You just learned to delete files. Let’s see how to remove directory in Linux.

How to remove directories in Linux

There is a command called rmdir which is short for remove directory. However, this rmdir command can only be used for deleting empty directories.

If you try to delete a non-empty directory with rmdir, you’ll see an error message:

There is no rmdir force. You cannot force rmdir to delete non-empty directory.

This is why I am going to use the same rm command for deleting folders as well. Remembering rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.

1. Remove an empty directory

To remove an empty directory, you can use the -d option. This is equivalent to the rmdir command and helps you ensure that the directory is empty before deleting it.

2. Remove directory with content

To remove directory with contents, you can use the recursive option with rm command.

This will delete all the contents of the directory including its sub-directories. If there are write-protected files and directories, you’ll be asked to confirm the deletion.

3. Force remove a directory and its content

If you want to avoid the confirmation prompt, you can force delete.

4. Remove multiple directories

You can also delete multiple directories at once with rm command.

Summary

Here’s a summary of the rm command and its usage for a quick reference.

Purpose Command
Delete a single file rm filename
Delete multiple files rm file1 file2 file3
Force remove files rm -f file1 file2 file3
Remove files interactively rm -i *.txt
Remove an empty directory rm -d dir
Remove a directory with its contents rm -r dir
Remove multiple directories rm -r dir1 dir 2 dir3

I hope you like this tutorial and learned to delete files and remove directories in Linux command line. If you have any questions or suggestions, please leave a comment below.

There can be various ways to remove a directory in any Linux Operating system. For example, if you are using a GUI of any Linux Operating system like GNOME or KDE, you can remove any directory by just right-clicking on the directory. But, when you are working with a stand-alone Linux server and only have a terminal to work with the Linux Operating system, this guide will help you remove a directory in the Linux Operating system’s terminal.

There are multiple commands for removing a directory in Linux, based on the need and different purposes. This post will let you know about all these commands and perform all of them on Ubuntu 20.04 LTS system. The commands that we are going learn and are usually used to remove a directory in Linux are

  • rmdir
  • rm

So, let’s dive in and learn how to remove or delete a directory in Linux and the different ways to use the above-mentioned commands.

Remove a directory using the ‘rmdir’ command

This rmdir command-line tool is specifically used for removing or deleting a directory in the Linux Operating system.

The syntax of using the rmdir command is pretty simple and easy. Just provide the directory name after the rmdir command, and the directory will be removed only if it is empty.

If the directory is empty, it will be removed immediately. Otherwise, it will prompt an error something like “failed to remove ‘directory_name’: Directory not empty”. This message ultimately means that the directory includes some files or subdirectories.

So, if you want to remove the directory and everything in it, you need to switch to the rm command.

Remove a directory using the ‘rm’ command

The rm command does not only used to remove or delete directories, but it is also used to delete both directories and files. It also comes with some additional flags which help in performing different tasks according to the requirement.

For example, to remove a directory and its subdirectories, the -r flag is used to remove a directory, its subdirectories, and files recursively.

This command will remove the directory and all the subdirectories and files in it immediately, only if the directory is not write-protected.

If the directory is write-protected, it will prompt for confirming the deletion process of a write-protected file. If you do not want to get prompted for the confirmation, you need to use the -f option for deleting a write-protected directory in Linux forcefully.

So, the command for deleting a write-protected directory and all the files and subdirectories in it will go like this:

Using the rm command, you can remove multiple directories in a single command as well. The command for deleting multiple directories would be like this:

This is how simply you can remove multiple directories in a single command using the rm command.

Conclusion

This post comprises detailed guidance on removing a directory in Linux using the rmdir and rm command. We have learned about various flags like a r flag that can recursively delete files and subdirectories. The -f flag can be used to delete a write-protected directory forcefully.

Suppose under the current directory, there are multiple sub-directories, and one is called A.

How to delete all sub-directories except A with Bash?

11 Answers 11

Bash has extended globbing (first test, then remove the echo):

This avoids some of the “scariness” of a typo in the other commands.

Please be aware to not be in the root (/) folder when running the rm * -rf command above.

edit

It should really be

to prevent find from recursing below A.

i usually do this by working up an ls command that gets it right first. i’m not at a unix machine, but something like:

Once you get it right, pipe it to a command

ls -lda “[^A]” | xargs rm -rf

Feel free to edit above if I’ve got my regular expression wrong.

If you want to be more flexible but manual you can do:

That way you can do general munging.

Here’s one way. Be careful with this sort of thing, though, it’s so powerful that it can only be used for good or evil.

Don’t use find as some people have shown with -exec and rm without passing -print0 to find and -0 to xargs. It will get confused on file names with spaces or newlines:

Instead use find -print0 with xargs -0 , ‘-exec command <> +’, or -delete if your find supports it.

In addition to the earlier example of:

you can also do:

to avoid having to cd some/subdir first.

find . ROOT_FOLDER. /* -type d | grep -v FOLDER_TO_KEEP | xargs rm -rf

Related

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

  • Blog
  • Facebook
  • Twitter
  • LinkedIn
  • Instagram

Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2022.5.26.42234

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Overview

Here we will show couple of ways/tips/hints on how to create multiple directories in Linux. The steps we want to cover in this article are how to create multiple directories in linux and sub-directories with the mkdir tool and for each individual example to create the directories with only one command. In other words, we want to run one mkdir command in terminal that will enables us to create multiple directories and sub-directories at once.

Just to recap first how we use mkdir to create a directory:

This single command will of course create just one singe directory where are your currently positioned in your Linux system. As we mentioned where are now going to show examples how to create multiple directories and sub-directories. In these examples we’re also going to use the mkdir tool. It’s still powerful to do all the directory creation examples we’re going to show.

Example 1. Create a parent directory with sub-directories

This is an example where we need to create a single parent directory(or upper level directory) where inside of it, we need to create multiple sub-directories, all at one with one command. To do this, run the mkdir tool in a command like this:

When the mkdir tool is ran like this, this will allow us the create multiple directories at once for this case.This is because of the -p argument, which designates the first in line directory(our main directory) as a parent directory which then enables us to add arguments for creating sub-directories inside of the main directory.

Small note, there must not be any spaces between the sub-directory names, otherwise the command will not work.

Example 2. Creating multiple directories without a parent directory

If you just need to create multiple directories in the in the currently positioned directory without having a main directory or creating a directory tree, we can just use brackets from example 1 and do just that:

Example 3. Creating a full path directories(sub-directory inside of sub-directory)

What does this mean is, if we have, or need to create a directory path like the following:

we can also achieve this with the mkdir tool, Run the mkdir as following:

Example 4. Bracket nesting

What we mean by this is – we can nest brackets from the example 1 and create multiple sub-directories inside sub-directories and all of them within the main directory.

With a command like this, you can instantly create a directory tree.

Example 5. Creating directories and setting permissions at the same time

Mkdir tool also has options to set the permissions as you want(as you have assigned) and create a new directory at the same time.

You can also combine this option with other examples we showed earlier and use to set a folder permission at once on multiple directories.

For an example, we’re now going to create a parent directory with multple sub-directories and have them all the same permissions:

Summary

We showed 5 different examples or use cases on how to create multiple directories in linux with the mkdir tool by running a single command in terminal. Mkdir is a powerful tool for creating directories and it has many more options for directory creation(which can be seen in the mkdir man page). The beauty of this tool that it’s options can be combined and the examples above we showed can also be combined to create directory trees with even more complexity or for many more use cases. it is highly suggested to go through the mkdir man page(usage documenation or manual). Link to the man page – mkdirmanpage.

Linux comes with several tools that can assist us in removing files. We always need to delete many files and folders based on a set of requirements. To complete our mission quickly, knowing a few basic commands and their variations is beneficial.

  • Use caution when using the commands below, particularly those that use regular expressions or search patterns with the find command. An incorrect expression or pattern will result in the deletion of important data/system files and non-intended files.
  • Often have a current copy of critical data and device files.
  • Use caution when running those commands, particularly if you’re using Sudo or as the superuser (root).

1. Remove file by using “unlink”:

Not so well-liked. We may use the unlink command to permanently delete a single file.

How to remove multiple subdirectories with one linux command

2. Delete a single file:

The rm command, which facilitates deleting one or more files simultaneously, is a more widely used command for removing files.

How to remove multiple subdirectories with one linux command

If the file is write-protected, rm will ask you to validate its deletion; otherwise, it will delete it without prompting. Using the “-i” flag to force rm to prompt for confirmation before removing a file:

How to remove multiple subdirectories with one linux command

The rm command deletes files without showing any messages. Using the rm command with the -v flag to see what the rm command is currently doing.

How to remove multiple subdirectories with one linux command

Using the -f flag to remove write-protected files without asking for clarification.

How to remove multiple subdirectories with one linux command

3. Multiple files can be deleted:

Bypassing multiple filenames as arguments to rm, you can delete multiple files.

How to remove multiple subdirectories with one linux command

Regular expressions are also supported by rm. If you want to delete all files with the name file-name-*, type:

How to remove multiple subdirectories with one linux command

Regular expressions may also be used to define different directories. We can use something like to delete three files that fit file-name-1, file-name-2, and file-name-3.

How to remove multiple subdirectories with one linux command

4. Delete the archive:

The rm command with the -d flag can be used to remove an empty directory.

How to remove multiple subdirectories with one linux command

Supported options for file deletion can also be combined with deleting the directory with the -d flag.

How to remove multiple subdirectories with one linux command

Using the -r flag to deleting a non-empty directory.

How to remove multiple subdirectories with one linux command

If you do not want a prompt before deleting the directory and its contents, use the -rf flag. This will remove everything inside the directory, including the directory itself, without any confirmation. Use caution especially when using as a root.

How to remove multiple subdirectories with one linux command

5. Locate and delete files:

We can use the locate command with various choices for more complicated specifications. To delete all files in a path specified by that follow a pattern .

Example:

How to remove multiple subdirectories with one linux command

We may slightly change the above command to delete everything that fits the sequence >, including directories within :

How to remove multiple subdirectories with one linux command

Internally, modern implementations of the find command support the delete feature. The -delete flag is used to override the rm instruction, while the –depth flag tells find to process the contents of the directory before the directory itself:

6. Empty files should be found and deleted:

You may use the following command to remove all empty directories within a given path dir-to-search:

How to remove multiple subdirectories with one linux command

Instead, use the following command to remove all empty files within a given path dir-to-search:

How to remove multiple subdirectories with one linux command

7. Permissions are used to locate and delete files:

We can now remove files based on special permissions, such as:

Consider the following scenario:

How to remove multiple subdirectories with one linux command

Easy (unlink), (rm), and (rmdir) commands are available in Linux, and they can be quickly expanded with regular expressions. For more specialized needs, you should use a variety of techniques such as (find) to accomplish your goals. Aside from the examples in this post, you can configure your quest by using find with any of the available flags.

Often run find commands without the rm or -delete flags and examine the output to determine which files or folders may be affected by the execution of a program. Backup setup and procedure are beneficial not just in the event of unintentional deletions, but also in the event of hardware errors and cyber-attacks.

There can be various ways to remove a directory in any Linux Operating system. For example, if you are using a GUI of any Linux Operating system like GNOME or KDE, you can remove any directory by just right-clicking on the directory. But, when you are working with a stand-alone Linux server and only have a terminal to work with the Linux Operating system, this guide will help you remove a directory in the Linux Operating system’s terminal.

There are multiple commands for removing a directory in Linux, based on the need and different purposes. This post will let you know about all these commands and perform all of them on Ubuntu 20.04 LTS system. The commands that we are going learn and are usually used to remove a directory in Linux are

  • rmdir
  • rm

So, let’s dive in and learn how to remove or delete a directory in Linux and the different ways to use the above-mentioned commands.

Remove a directory using the ‘rmdir’ command

This rmdir command-line tool is specifically used for removing or deleting a directory in the Linux Operating system.

The syntax of using the rmdir command is pretty simple and easy. Just provide the directory name after the rmdir command, and the directory will be removed only if it is empty.

How to remove multiple subdirectories with one linux command

If the directory is empty, it will be removed immediately. Otherwise, it will prompt an error something like “failed to remove ‘directory_name’: Directory not empty”. This message ultimately means that the directory includes some files or subdirectories.

So, if you want to remove the directory and everything in it, you need to switch to the rm command.

Remove a directory using the ‘rm’ command

The rm command does not only used to remove or delete directories, but it is also used to delete both directories and files. It also comes with some additional flags which help in performing different tasks according to the requirement.

For example, to remove a directory and its subdirectories, the -r flag is used to remove a directory, its subdirectories, and files recursively.

This command will remove the directory and all the subdirectories and files in it immediately, only if the directory is not write-protected.

If the directory is write-protected, it will prompt for confirming the deletion process of a write-protected file. If you do not want to get prompted for the confirmation, you need to use the -f option for deleting a write-protected directory in Linux forcefully.

So, the command for deleting a write-protected directory and all the files and subdirectories in it will go like this:

Using the rm command, you can remove multiple directories in a single command as well. The command for deleting multiple directories would be like this:

This is how simply you can remove multiple directories in a single command using the rm command.

Conclusion

This post comprises detailed guidance on removing a directory in Linux using the rmdir and rm command. We have learned about various flags like a r flag that can recursively delete files and subdirectories. The -f flag can be used to delete a write-protected directory forcefully.

This is where the concept of recursive deletion comes into play. Recursive deletion aims to delete all the files and directories within a subdirectory. Generally, whenever you attempt to delete any file or a directory within any operating system, the OS prompts you to provide confirmation to prevent accidental deletion of important files or directories. However, if you are 100% sure of what you are going to delete, and there is a large number of files to be deleted, then you might find it troublesome to provide confirmation for every file or directory.

In this case, you can remove a directory recursively without being prompted by the OS for confirmation every time. This article explains how to remove a directory recursively without prompting the user for confirmation in Linux Mint 20.

To remove a directory recursively in Linux Mint 20 without prompting the user for confirmation, the following series of steps should be performed.

Step 1: List Contents of Directories

We have created two sample directories, namely, Directory1 and Directory2, in our Home directory to demonstrate this method of removing directories recursively in Linux Mint 20. Directory1 contains two subdirectories, named D1 and D2, whereas Directory2 contains the file named D5. We will show you the contents of our Home directory so that you can verify that Directory1 and Directory2 exist in our Home directory. To list the contents of the Home directory, we will run the following command in our terminal:

You can see from the output of this command that Directory1 and Directory2 exist in our Home directory, as highlighted in the image below. We performed this step so that you can easily verify the deletion performed in Step 4 of this method.

Next, we will show you the contents of our Directory1 by running the following command in the terminal:

Here, you can give the path of any directory of which the contents you would like listed.

The contents of Directory1 are shown in the image below:

Finally, we will show you the contents of our Directory2 by running the following command in the terminal:

Here, you can give the path of any directory of which the contents you would like listed.

The contents of Directory2 are shown in the image below:

Step 2: Remove a Single Directory Recursively without Prompting the User for Confirmation

To remove a single directory recursively without prompting the user for confirmation, run the following command in your terminal:

Here, replace “PathOfTheDirectoryToBeDeleted” with the exact path of the directory that you intend to delete. In our case, the directory is /home/aqsa_yasin/Directory1. The “-rf” flag, along with the “rm” command, removes a directory recursively without prompting the user for confirmation.

Step 3: Remove Multiple Directories Recursively without Prompting the User for Confirmation

If you wish to remove multiple directories recursively at a time without prompting the user for confirmation, then skip Step 2 and, instead, run the following command in your terminal:

Here, replace “Path1” and “Path2” with the exact paths of the directories that you intend to delete. In our case, we only wanted to delete two directories, i.e., Directory1 and Directory2. However, you can remove as many directories as you want using this command simply by stating the paths of the directories, separated by spaces, following the “rm –rf” command.

Step 4: Verify Deletion of Specified Directories

After executing the command in Step 3, ideally, our Directory1 and Directory2 should be removed, along with all their subdirectories, from our Home directory. We can always confirm whether the deletion process has successfully taken place by listing down the contents of our Home directory. We can do so by running the following command in the terminal:

This time, in the output of this command, we will no longer be able to see Directory1 and Directory2 in the Home directory, as shown in the image below. This indicates that the specified directories have been removed successfully.

How to remove multiple subdirectories with one linux command

Conclusion

By using the method prescribed in this article, you can remove a single directory or multiple directories recursively without prompting the user for confirmation in Linux Mint 20. With this method, you can get rid of all the traces of a directory at once, including all the subdirectories and files within it, without constantly needing the user to provide consent. In this way, you can easily and quickly free up your system’s storage space for more important files and directories. I hope that, by following this article, you are now in the position to delete directories recursively without prompting the user for confirmation.

About the author

How to remove multiple subdirectories with one linux command

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

rm command in UNIX stands for remove and by default is used for removing files. It is simple but a powerful command especially when used with options such as -rf which allow it to delete non-empty directories forcefully.

Removing Files in Linux:

The rm command, By default, cannot remove Directories and only works on files.

How to remove multiple subdirectories with one linux command

We use mkdir and touch commands to make directories and text files respectively, and ls command to list files in the current working directory.

Removing Multiple Files in Linux:

To remove multiple files at once, we can write file names separated by spaces after the rm command or use a pattern to remove multiple files that fit the pattern.

How to remove multiple subdirectories with one linux command

removing multiple files

Removing a Directory in Linux:

To remove a directory, you can use the -r or -R switch, which deletes a directory recursively including its content (subdirectories and files). If it is an empty directory you can also use rmdir command.

Removing Files with Confirmation Prompt:

To get a confirmation prompt while deleting a file, use the -i option.

removing files with confirmation

Removing Directories with Confirmation Prompt:

To get a confirmation prompt while deleting a directory and its sub-directories, use the -R and -i option.

removing Directories with confirmation

Removing File or Directory Forcefully:

To remove a file or directory forcefully, you can use the option -f force a deletion operation without rm prompting you for confirmation. For example, if a file is unwritable, rm will prompt you whether to remove that file or not, to avoid this and simply execute the operation.

When you combine the -r and -f flags, it means that you recursively and forcibly remove a directory (and its contents) without prompting for confirmation.

Here, we created a text file and directory and made it read-only by taking its write access using chmod command.

How to remove multiple subdirectories with one linux command

Showing Information While Deletion:

To show more information when deleting a file or directory, use the -v option, this will enable rm command to show what is being done on the standard output.

How to remove multiple subdirectories with one linux command

Is rm -rf Command bulletproof?

rm -rf as powerful as it is, can only bypass read-only access of nested files and not directories. To delete the directory ( B/C ) we need to access it through superuser privileges.

How to remove multiple subdirectories with one linux command

It is not recommended to use this command as a superuser if you are not 100% sure what you are doing as you can delete important files.

The “rm -Rf /” Command:

You should always keep in mind that “rm -rf” is one of the most dangerous commands, that you should never run on a Linux system, especially as a root. The following command will clear everything on your root(/) partition.

There are checks to prevent root deletion but the additional option of –no-preserve-root bypass that failsafe. It’s a meme on the internet that is equivalent to deletion of system32 in your windows os C:\ drive.

You should not use the above command in any case whatsoever, for curious folks I did it use the command with –no-preserve-root. And after some deletion of important files and directories, I was left with nothing but hanged up output shown below.

Create Alias for rm Command in Linux:

To permanently use -i option for safety, add an alias in your $HOME/.bashrc file.

Source your .bashrc file as shown or open a new terminal for the changes to take effect.

Now, whenever you execute rm, it will be invoked with the -i option by default (but using the -f flag will override this setting).

How to remove multiple subdirectories with one linux command

Does rm actually Delete a File?

rm doesn’t actually delete files but rather unlink them (free the memory for further use). To permanently delete the data you can use the shred or dd command.

This tutorial is about How to Execute Multiple Commands in Linux at Once. Recently I updated this tutorial and will try my best so that you understand this guide. I hope you guys like this blog, How to Execute Multiple Commands in Linux at Once. If your answer is yes after reading the article, please share this article with your friends and family to support us.

  • Check How to Execute Multiple Commands in Linux at Once
  • Option one: the semicolon operator (;)
  • Option two: the logical AND operator (&&)
  • Option three: the logical OR operator (||)
  • Combine multiple operators
  • Final remarks: How to Execute Multiple Commands in Linux at Once

Check How to Execute Multiple Commands in Linux at Once

If you use Linux on a daily basis, you know that the command line is the most dominant tool when working with files, installing and configuring system software, and running them. It becomes even more systematic if you run two or more commands at the same time on the command line and save a great deal of time.

Here, we will look at the different methods in which we can productively merge and run multiple Linux commands on a single line.

Option one: the semicolon operator (;)

The semicolon (;) operator allows you to run multiple commands in succession, regardless of whether each previous command succeeds. For example, open a Terminal window (Ctrl + Alt + T on Ubuntu and Linux Mint). Then type the following three commands on one line,

separated by semicolons and press Enter. This will give you a list of the current directory (ls), find out what directory you are currently in (pwd), and display your login name (whoami) in one go.

You also don’t have to put spaces between the semicolons and the commands. You can enter all three commands as ls; pwd; whoami. However, spaces make the combined command more readable, which is especially useful if you are placing a combined command in a shell script.

Option two: the logical AND operator (&&)

If you want the second command to run only if the first command is successful, separate the commands with the logical AND operator, which is two symbols and (&&). For example, we want to create a directory called MyFolder and then change to that directory, provided it was created correctly. So, we type the following on the command line and hit Enter.

The folder was created successfully so the cd command was run and now we are in the new folder.

We recommend using the logical AND operator instead of the semicolon operator most of the time (;). This ensures that you don’t do anything disastrous. For example, if you run a command to change to a directory and then force delete everything in that directory recursively (cd / some_directory; rm -Rf *), you could end up messing up your system if the directory change didn’t happen. Not that we recommend that you run a command to unconditionally delete all files in a directory at once.

Option three: the logical OR operator (||)

Sometimes you may want to run a second command only if the first command doesn’t work. To do this, we use the logical OR operator, or two vertical bars (||). For example, we want to check if the directory MyFolder ( ) and create it if you don’t (mkdir

/ MyFolder). So, we type the following command at the command prompt and hit Enter.

Make sure there is a space after the first bracket and before the second bracket or the first command that checks if the directory exists will not work.

In our example, the MyFolder directory does not exist, so the second command creates the directory.

Combine multiple operators

You can also combine multiple operators on the command line. For example, we first want to check if a file exists ( ). If so, we print a message on the screen indicating it (echo “The file exists”). If not, we create the file (tap

/ sample.txt). So, we type the following at the command prompt and hit Enter.

In our example, the file did not exist, so it was created.

Here is a useful summary of each of the operators used to combine commands:

  • TO ; B – Run A and then B, regardless of A’s success or failure
  • A && B – Run B only if A was successful
  • A || B – Run B only if A failed

All of these command join methods can also be used in shell scripts on both Linux and Windows 10.

Final remarks: How to Execute Multiple Commands in Linux at Once

I hope you understand this article, How to Execute Multiple Commands in Linux at Once. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.

Home » SysAdmin » How to Remove (Delete) a File or Directory in Linux

How do I delete a file in Linux using the command line option? How can I remove a Linux directory?

Deleting files and directories is a necessary task when working with Linux. In this guide, learn how to remove files and directories from the command line in Linux using the RM Command.

How to remove multiple subdirectories with one linux command

  • A command line / terminal window (Ctrl-Alt-T or Ctrl-Alt-F2)
  • A user account with sudo privileges (optional)

Note: If you feel that a directory is misplaced and you do not want to remove it, try moving it to a different place. To learn how, visit our post How to Move Directories in Linux.

How To Remove or Delete Linux Files

The rm command deletes files in a Linux. The command unlinks the data from the file name, allowing the user to overwrite on that particular storage space.

To delete a single file, entering the following in the command line:

The rm command can be used to delete more than one file at a time:

Wildcards can be used with this command.

For example, to delete all files with the .bmp filename, enter:

This method is also used to delete all files that contain a string of characters:

This will erase any file that has the word sample in the name.

The system will search the current directory for the file you want to remove.

To delete a file in a different directory, either switch to that directory first:

Or you can specify the file location in a single command directly:

Note: Once the rm command has deleted a file, you will not be able to access it. The only way to retrieve a file would be to restore it from a backup (if one is available).

rm Command Options

You can adjust the way the rm command works by adding options. An option is a hyphen, followed by one or more letters that stand for commands.

If you’re deleting multiple files, add a confirmation prompt. Use the –i option to use an interactive dialog:

Confirm the deletion of files by typing ‘yes’ or ‘no.’

To display the progress of the deletion with the v or verbose command:

The output confirms that the file test.txt has been successfully removed.

To force the removal of a file that’s write-protected, use the –f option:

To use sudo privileges for a file that says Access denied and delete it:

How to Delete a Directory in Linux

A linux directory (or folder) can be empty, or it can contain files. To remove a directory in Linux, use one of the following two commands:

  • rmdir command – removes empty directories/folders
  • rm command – removes a directory/folder along with all the files and sub-directories in it

Remove Directory Linux with rm Command

By adding the -r (-R) option to the rm command, you can remove a directory along with all its contents.

To remove a directory (and everything inside of it) use the –r option as in the command:

This will prompt you for confirmation before deleting.

To remove a directory without confirmation:

Also, you can delete more than one directory or folder at a time:

Remove Directories in Linux with rmdir Command

Remember, the rmdir command is used only when deleting empty folders and directories in Linux. If a specified directory is not empty, the output displays an error.

The basic syntax used for removing empty Linux folders/directories is:

Additionally, you can delete multiple empty directories at once by typing:

If the command finds content in one of the listed directories, it will skip it and move on to the next one.

Note: To permanently delete a file in Linux by overwriting it, use the shred command.

With this tutorial, deleting files and directories in Linux was made easy. The rm and rmdir commands are flexible with many options available.

How to remove multiple subdirectories with one linux command

Updated on March 31, 2021

How to remove multiple subdirectories with one linux command

Get your SSD VPS

Starting from $7.95/month.

Linux has lots of commands to learn. This how-to guide will explain to you how to Delete a Directory in Linux. Do you need to delete a directory in Linux? what is a directory? how you can delete it? you know, a directory is a group of files.

Linux command lines let you delete directories with different conditions. For example, you can delete empty directories or search and delete them by name, or select a directory and delete it with all its content.

There are two commands to delete a file in Linux including the “rmdir command and “rm command. Here we explain the most popular possibilities for these commands.

Want a better way to host your websites and web apps? Developing something new? Simply don’t like Windows? That’s why we have Linux VPS.

How to Delete an Empty Directory in Linux?

How to remove multiple subdirectories with one linux command

If you have an empty directory in Linux that you want to delete, use this command:

This command is useful because it only removes the empty directory. Run the command as:

In which “dir-name” is the name of your directory. If the directory is not empty, you will receive the message below:

How to Delete the Directory, File, and Subdirectory in Linux?

If you want to delete the directory, regardless of its content, you need to use the “rm” command. Keep in mind that the removal will be permanent.

Linux tries to confirm the deletion of the write-protected files. To skip the confirmation, prompt the command below:

If you want to receive one confirmation, use the command below:

To remove multiple directories and their content, use the command below. This command removes directories dir1, dir2, and dir3.

You can also name-match and delete directories based on that. In the example below, you delete every directory starting with dir.

Remember only owners can delete their directories. If you are the sysadmin, prompt the command bellow:

How Do I Delete All Files and Subdirectories in a Directory in Linux?

Do you want to keep the directory but remove all its content? To delete all files within the directory, run:

To delete files, subdirectories, and hidden files and directories, run:

Delete a Directory in Linux by Using the Find Command

You can prompt the system to search for certain directories and delete them. For example, the command below finds all directories named dir and deletes them:

  • -type d : Only search within directories.
  • -iname ‘dir’: Search for directories named ‘dir’.
  • -delete: Delete all found directories.

Find and Remove All Empty Directories in Linux

You can use the find command to delete all empty categories at once. For example, the line below searches a directory name dir and deletes all empty folders:

  • -type d : Only search within directories.
  • -iname ‘dir’: Only search within the directory named ‘dir’.
  • -empty : Search for empty directories.
  • -delete : Delete all found empty directories.

What to Do If Getting Permission Denied Error Message While Removing a Directory?

Owners and sub-admins can delete a directory in Linux. If you delete a directory and receive a permission denied error, run the following syntax:

When prompted, you need to provide a root user or “sudo” user password.

Want a better way to host your websites and web apps? Developing something new? Simply don’t like Windows? That’s why we have Linux VPS.

Conclusion

Linux allows you to not only delete a directory, but also search for the empty directory with a specific name, and delete them. You can also keep the directory, but delete all its content. All this is possible using the commands above. In this article, we explained how to delete a directory in Linux. Further, we showed you how to delete the file, and subdirectory in Linux.

How to remove multiple subdirectories with one linux command

Introduction

A symbolic link, commonly referred to as a symlink, is a file that points to another file or directory. In Windows, it’s similar to a shortcut. A symlink can point to a file or directory on the same filesystem or partition as the original.

Using the rm , unlink , and locate commands, we’ll teach you how to remove (delete) symbolic links in Linux/UNIX systems.

Prerequisites

You must have write permissions on the directory that contains the symlink in order to remove it. Otherwise, you’ll get an error message that says “Operation not permitted.”

The file that symlink references remain unaffected when it is removed.

The ls -l command can be used to determine whether a file is a symbolic link and to locate the file or directory to which the symbolic link points.

The letter “l” in the first character indicates that the file is a symlink. The “->” symbol denotes the file to which the symlink refers.

Remove Symbolic Links with rm

The rm command deletes the files and folders specified.

Invoke the rm command with the symbolic link name as an argument to delete a symlink:

The command terminates with a zero and produces no output if it succeeds.

You can use rm to erase multiple symbolic links at once. To do so, supply the symlinks’ names as arguments, separated by a space:

Use the -i option to be prompted before deleting the symlink:

Type y and press Enter to confirm.

Do not attach the / trailing slash to the end of a symbolic link pointing to a directory. Otherwise, you will receive the following error:

If the argument’s name ends in / , the rm command considers the file to be a directory. The issue occurs because rm cannot delete directories when used without the -d or -r options.

When eliminating symbolic links with rm , always use the -r option to be safe. For instance, if you type in:

The target directory’s contents will be removed.

Remove Symbolic Links with unlink

The unlink command removes a link between two files. unlink , unlike rm , only takes one parameter.

Run the unlink command with the symlink name as an argument to erase a symbolic link:

If the command is successful, no output is displayed.

Because unlink cannot remove directories, do not append the / trailing slash to the end of the symlink name.

Find and Delete Broken Symbolic Links

The symbolic file will be left dangling if you delete or relocate the source file to a different location (broken).

Run the following command to detect all broken symbolic links in a particular directory:

All broken links in the directory and its subdirectories will be listed by this command.

If you want to find symlinks in subdirectories but not in the main directory, use the -maxdepth 1 option.

Once you’ve located the broken symlinks, you may either manually remove them using rm or unlink , or use the find command’s -delete option:

Conclusion

Use the rm or unlink commands with the name of the symbolic link as an argument to remove a symbolic link. Do not apply a trailing slash to the symlink name when removing a symbolic link that points to a directory.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

How to remove multiple subdirectories with one linux command

One of the basic file system administration tasks involves creating, modifying, and deleting different types of files and directories. Knowing some basic tools and concepts for file deletion comes in handy and can save you time.

Linux offers several tools that can help us accomplish file removal tasks. Often we need to remove not just a single, but a bunch of files and directories, based on some criteria. It is helpful to know a few common commands and their combinations to get our task done easily.

  • Use the below commands with caution especially the ones which use regular expressions or some search patterns with the find command. An incorrect expression or pattern may delete essential data/system files or simply non-intended files.
  • Always secure the latest backup of important data and system files.
  • Take caution before running such commands especially when running it with Sudo or as superuser (root).

Remove using unlink

Not so popular. To remove a single file permanently, we can use unlink command.

Remove single file

There’s a more commonly used command for removing files, i.e., rm command, which supports removing one or more files simultaneously.

rm prompts you to confirm file deletion for files that are write-protected else it proceeds to directly remove the file. To make rm always prompt before deleting a file, use -i flag:

rm command deletes without displaying any messages on the screen. To list what rm command is actually doing, use rm with -v flag.

To remove write-protected files without prompting for confirmation, use -f flag.

Remove multiple files

Multiple files can be removed by specifying multiple filenames to rm as arguments.

rm also supports regular expressions. If you want to delete all files named file-name-* , you can use:

We can also specify multiple files using regular expressions. If we want to delete three files that match file-name-1 , file-name-2 and file-name-3 , we can use something like:

Remove directory

An empty directory can be deleted using rm command with -d option.

Options supported for file removal can also be combined with directory removal using -d flag as well. For example:

To delete a directory that is non-empty use -r flag.

If you don’t want any prompts before deleting the directory and its contents, use -rf flag. This WILL delete everything inside the directory including the directory itself without any confirmation. Do use caution while using it especially as root.

Find and remove files

For more complex requirements, we can use the find command with different options. To remove all files matching pattern inside a path given by

If we want to remove anything that matches a pattern including directories inside , we can slightly modify the above command as:

Modern versions of the find command support the delete function internally. Here -delete flag replaces rm command while -depth instructs find command to first process the contents of the directory before the directory itself :

The above example is more efficient when working with a large number of files as it doesn’t spawn a new external process for rm command for each matched file. But not all versions of find command (yet) may support -delete flag. As an alternate, we have the option to use xargs command with find as shown in the next example below:

Or use exec command’s + terminator to chain together everything found by find command like xargs:

By default, the find command uses -print flag which we usually skip writing. With xargs , we need to avoid new-line character between each filename. As such -print0 flag instructs find to print null character after every found filename.

Similarly, we specify -0 flag with xargs so that both commands couple up. rm command here deletes the file passed on to by piped find input. For example, the below command will search and remove all *.tmp files from the current user’s home directory (given by

find command offers several ways to search files and directories including ownership, permission, timestamp, etc. We’ll be covering a few such ways that can help us in specific file removal tasks.

Find and remove files by a user

To delete everything inside a given directory owned by a specific user , use:

In the above example, -mindepth flag with value 1 prevents the directory given by from deletion if it matches the find pattern and criteria. Or to delete everything inside a given directory belonging to a particular group , use:

If you do not want to traverse the sub-directories under the search path, you also have the option to use -maxdepth with appropriate directory depth value.

Here’s an example run:

If you want to specify user-id and group-id instead, you can try something like:

Find and remove empty directories

To delete all empty directories inside a given path , you can use:

Instead, to delete all empty files inside a given path , use:

Find and remove files older than X

Sometimes, you may need to delete files older than x days. find has options to read file’s creation ( ctime ), access ( atime ) and modification ( mtime ) times. We can use mtime option with find to find and delete files modified more than x days ago.

For instance, to delete all files with extension log inside /var/tmp with a modification time of 30 days ago or more, we can use:

Find and remove files by permission

Now, we can also delete files based on some specific permission, like:

Or if we want to use a symbolic form for permission, use:

Summing Up

Linux offers unlink , rm and rmdir commands that are simple and can be easily extended with the use of regular expressions. For more advanced needs, you have the option to use a combination of tools like find and xargs to achieve what you need. Other than the examples in this article, you’ve got the option to use find with any of its available flags to customize your search. Refer man pages for respective commands to learn more about them.

Always run find commands without rm or -delete flag and analyze its output to know which files or directories will be impacted by the execution of an actual command. Having proper backup configuration and policy helps not only in case of accidental deletions but also in cases of hardware failures and cyber-attacks.

Spring cleaning is not only necessary for homes, Your Linux Mint 20 file system also needs to be decluttered regularly. An organized file system will save you or the system administrator a lot of headaches in the long run.

In this article, you will learn how to remove your directories to organize your file system.

Pre-requisites

  • Linux system, I’ll use Linux Mint 20 here.
  • Terminal access
  • A user account with sudo privileges.

Note: All the methods used in this tutorial will remove the directory and its files permanently and can not be recovered, so make sure to back up the important files and directories before proceeding.

Find Your Current Working Directory

First of all, you will need your directory name/path to follow this tutorial. In case you do not know your current directory, run the following command in the terminal to find out your directory path.

List Down Directories/Files in Your Current Directory

List down all the directories and files in your current directory and verify the deleted directories after every with the following command.

Remove Directories Using rmdir Command

To delete the directory, run the following command.

Note: The rmdir command will only delete the empty directories. Otherwise, it will give the following error output.

Remove Directories Using rm Command

The rm command can delete any directory, unlike the rmdir command which only works for empty directories.

To remove the directories, use the following command syntax with the -r option.

You can see in the above snippet that test1 is deleted and not listed in the directory.

To delete multiple directory at once, run the following command.

If you want confirmation on deleting a directory, run the rm command with the ‘I’ option.

Remove Directories Using find Command

If you are aiming at deleting multiple directories and do not know exactly where targeted files are at. Or simply do not want to list long commands with a long line of argument then you should use the find command to simultaneously search directories for target and delete them using one command.

For example, use the following command to delete all the directories with a certain name pattern.

Note: Remember to set your directory name pattern before running the above command.

Similarly, run the following command to delete all the empty subdirectories in a particular directory.

How to solve the permission denied error?

If any directory or file in the directory has restrictions like read-only etc, You will get the following outcome with a permission denied msg.

In that case, you simply need to run the respective command with a sudo/root privileged account.

Let us say you have 100+ files in a folder. You want to delete all of them except one or few specific files. How would you do it? You can copy the files you wanted to keep, and save them in a different location, and then delete the rest of the files or the entire folder. But wait, I know an easiest way to do this. You can remove all files in a older except one specific file or certain type of files in one go with a single line command. Want to know how? Read on.

Remove All Files In A Folder Except One Specific File

Let us picture the following example. We have a folder called ‘test’ that contains 10 text files.

Now, I want to delete everything in this folder except file10.txt.

There might be many commands to do this. But these are the five commands that I am aware of.

First, go to the test folder:

And run the following command:

The above command will delete all files in the test folder except file10.txt file.

You can also use find command to delete everything but specific one. The following command will delete all files in the current folder (i.e test in our case) except file10.txt.

As you see in the above example, the test folder contains same type of files i.e .txt files. What would you do if the folder has different type of files like .mp3, .doc, .pdf etc.? It’s also easy to keep particular type of files in a folder and delete everything else.

Let us say our test folder contains three .txt files, three .mp3 files, three .doc files, and one .pdf file.

Sample output:

As you in the above output, I have four different type of files (pdf, txt, mp3, doc) in the test folder. I’d like to keep the files that has .doc extension and remove everything else. Here is how I can do this:

Now, let us list the file contents using command:

Sample output:

The above command delete everything in the folder except the files that has extension .doc.

Similarly, you can keep two or more particular types of files and remove everything else. Say for example, the following command will keep the files that contains .doc and .mp3 extensions.

Now, you will see the mp3 and doc files are not deleted.

Sample output:

These are just ten different type of files. Just image you have hundreds of files. It would be harder to find each file type and delete them manually. This trick will do the job in just one or two seconds.

Please be very careful while using these commands. Double check the directory path before deleting files to avoid accidental deletion of important files.

Suggested read:

Hope this helps. If you find this guide useful, please share it on your social, professional networks and support us.

How to remove multiple subdirectories with one linux command

Linux is a popular open source operating system, and its features are often available in your development environment. If you can learn its basic commands, it’ll make your life as a developer much easier.

In this guide you will learn how to delete directories and files from the Linux command line.

The rm (short for remove) command is pretty useful. Let’s learn its syntax and look at a few examples to see it in action.

rm Command Syntax

The syntax is shown below, with args being any number of arguments (folders or files).

Without options you can use it to delete files. But to delete directories you need to use the options for this command.

The options are as follows:

  • -r , “recursive” – this option allows you to delete folders and recursively remove their content first
  • -i , “interactive” – with this option, it will ask for confirmation each time before you delete something
  • -f , “force” – it ignores non-existent files and overrides any confirmation prompt (essentially, it’s the opposite of -i ). It will not remove files from a directory if the directory is write-protected.
  • -v , “verbose” – it prints what the command is doing on the terminal
  • -d , “directory” – which allows you to delete a directory. It works only if the directory is empty.

Linux rm Command Example

Let’s take a project_folder directory as an example. It has these files and folders inside:

Let’s use this directory to show how the various options work.

You can add the option -v to all commands so that it will write down step by step what’s going on.

So, let’s start with the first option, -r . You just learned that this removes files and folders recursively. You can use it like this rm -r project_folder or also rm -rv project_folder as the verbose option.

How to remove multiple subdirectories with one linux command

It has deleted the project_folder directory and everything inside it, in the order shown.

Let’s recreate the folder and try again.

What happens if you don’t use the -r option and you try to delete the directory anyway? It will not allow it and will instead show an error:

To delete directories you can use the -d option, but if you try to use it in this case it will give an error as the folder is not empty.

The -i option make so that it asks about each action individually.

And you need to press y or n and then Enter after each query.

If you select y for all queries it will delete everything:

How to remove multiple subdirectories with one linux command

If instead you decide to not delete some files or folders, with n it will keep those files and continue with the rest:

How to remove multiple subdirectories with one linux command

The last option we haven’t seen so far is -f , which will suppress errors.

For example writing as below you would be trying to delete two non existing files – there is not a rat.png file, and dog.pmg has a typo and it gives two errors. With the -f option, you will not see the errors.

How to remove multiple subdirectories with one linux command

The Linux command line is pretty useful if you’re a developer. In this article, you have seen one of its possible commands, rm , that you can use to delete directories and files.

Enjoy this new tool in your arsenal!

Moderator and staff author for freeCodeCamp. Guiding localization efforts for Italian.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons – all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Normally on this blog I tend to write about more complicated tasks or fancy Linux tricks and completely overlook some of the most basic tasks that a SysAdmin needs to know. Today I have decided that I will make my blog a little more comprehensive and add some posts with some of the basics.

Along with this I will be starting a new category, called Sysadmin Basics and I will try to post an additional article each week that covers some of the more basic concepts and commands used by Linux and Unix Sysadmins.

Remove Directories with the rmdir command

The rmdir command is used to delete and remove empty directories. I bolded empty as it is important to note that rmdir will only remove a directory if there are no files within that directory. If you want to remove a directory and all files within that directory, skip down to the rm section of this article.

Remove a single empty directory

Remove multiple empty directories (in a single tree)

While rmdir will not remove directories with files in it; rmdir will recursively remove a directory tree that has no files. In the example somedir only has directory a within it, and the a directory only has b which only has c and so on.

Remove multiple empty directories

The above command will also fail if there are multiple directories in one single directory, to handle that scenario you can list the directories individually and include the –ignore-fail-on-non-empty flag.

Without the –ignore-fail-on-non-empty flag the command will still print that somedir is not empty even though it removes somedir. This is due to the fact that both command line arguments ask rmdir to remove somedir and rmdir cannot remove that directory until the last step.

Removing Files and Directories with the rm Command

While the rmdir command is solely for directories the rm command can remove both files and directories. With the right combination of flags rm will also remove entire directories, files and all.

Remove a file

On it’s own rm will not prompt a user before removing a file; to keep systems safe from accidental file removals some distributions of Linux will ship with an alias for rm with the default .bashrc file. This alias gives the interactive -i flag for rm, this tells rm to prompt the user before removing files and directories.

Remove a file without being prompted

While you can simply unalias the rm alias, a simpler and generally used method to remove files without being prompted is to add the force -f flag to the rm command. It is advisable that you only add the force -f flag if you really know what you are removing.

Remove a file without being prompted and with verbosity

If you don’t want to be prompted for each file removable but also want to keep an eye on rm in case the command starts removing unexpected files, you can simply add the verbose -v flag.

Remove multiple files

There are many ways to remove multiple files, one method is to simply list each file you want to remove.

Removing multiple files with a wildcard

The bash command line supports wildcards and regex statements. A simplier way to remove all files that end in the word file is to simply state *file . I suggest being cautious with wildcards as it is entirely possible to remove a file without meaning to.

Remove files using a regex

Another common method of deleting files is to use regex statement, the below would remove anything that looks like files-0 through files-9 but would not remove files-a or files-list.

Remove a directory and all of it’s contents with rm

If you want to simply remove an entire directory and all of the contents within, including both files and directories the easiest method is to add the recursive -R flag to rm. If you are in any way unsure of what you are doing than drop the force -f and replace it with verbose -v or interactive -i .

How to Remove Files

  1. To delete a single file, use the rm or unlink command followed by the file name: unlink filename rm filename. .
  2. To delete multiple files at once, use the rm command followed by the file names separated by space. .
  3. Use the rm with the -i option to confirm each file before deleting it: rm -i filename(s)
  1. How do you run a RM?
  2. What is RM RF command?
  3. What will rm * do?
  4. Is rm command?
  5. Can you sudo rm?
  6. What is the difference between RM and RM?
  7. How do I use the rm command in Windows?
  8. What does RM mean in Apple?
  9. What does cp command do in Linux?
  10. What is RM in Python?
  11. Is command used for?
  12. How do I rm a file in Linux?

How do you run a RM?

To run rm command, type rm followed by filename. Please remember, that by default rm will not ask any confirmation. Here’s an example of rm in action.

What is RM RF command?

The rm -rf command is one of the fastest way to delete a folder and its contents. . rm command in Linux is used to delete files. rm -r command deletes the folder recursively, even the empty folder. rm -f command removes ‘Read only File’ without asking. rm -rf / : Force deletion of everything in root directory.

What will rm * do?

rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names).

Is rm command?

In computing, rm (short for remove) is a basic command on Unix and Unix-like operating systems used to remove objects such as computer files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets, similar to the del command in MS-DOS, OS/2, and Microsoft Windows .

Can you sudo rm?

If you type sudo rm , you are running the rm command with root privileges instead of your user ones. So the answer can be: “rm -f *” -> delete all files in current directory which you have rights to without asking.

What is the difference between RM and RM?

rm removes files and -rf are to options: -r remove directories and their contents recursively, -f ignore nonexistent files, never prompt. rm is the same as “del”. . rm -rf adds the “recursive” and “force” flags. It will remove the specified file and silently ignore any warnings when doing so.

How do I use the rm command in Windows?

/s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree.
.
There are three scenarios where rm -rf is commonly used where it is expected to return 0 :

  1. The specified path does not exist.
  2. The specified path exists and is a directory.
  3. The specified path exists and is a file.

What does RM mean in Apple?

rm means in short ‘remove.

What does cp command do in Linux?

cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name.

What is RM in Python?

Python List remove() The remove() method removes the first matching element (which is passed as an argument) from the list.

Is command used for?

The IS command discards leading and trailing blank spaces in the terminal input and converts embedded blank spaces to single blank spaces. If the text includes embedded spaces, it is composed of multiple parameters.

How do I rm a file in Linux?

How to Remove Files

  1. To delete a single file, use the rm or unlink command followed by the file name: unlink filename rm filename. .
  2. To delete multiple files at once, use the rm command followed by the file names separated by space. .
  3. Use the rm with the -i option to confirm each file before deleting it: rm -i filename(s)

This brief tutorial shows students and new users how to use the rm command on Ubuntu to delete or remove files and folders, including directories.

We previously showed you how to use the rmdir command on Ubuntu. rmdir command is used to delete folders or directories and not files.

The rm command does both.

If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS…. It’s a great Linux operating system for beginners.

Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other devices…

During your introduction, you will find that Linux isn’t so different than Windows and other operating systems in so many ways, especially when it comes to using the system to get work done.…

Both Ubuntu and Windows systems allow you to be productive, easy to use, reliable and enable you to install and run thousands of programs from gaming to productivity suite software for individuals and businesses.

However, when you’re learning to use and understand Ubuntu Linux, you should also learn how to use the command line to terminal. Most Linux users should be able to do some basic command line tasks. This tutorial is going to show you how….

When you’re ready to learn how to use the rm commands, follow the guide below:

About rm command:

The rm command is a command utility use to remove to remove or delete files and directories on Linux systems, including Ubuntu. It’s one of the most common commands that Linux users get to use on a daily basis.

Syntax:

The syntax is the rule and format of how the rm command can be used. These syntax options can be reordered, but a straight format must be followed.

Below is an example syntax of how to use the rm command.

rm [OPTION]. [FILE].

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the commands. they are separated by spaces and followed after the commands.

Below are some options of the rm command:

FILE. Replace FILE.. with the file(s) you want to remove or delete
-f, –force Use the -f or –force to ignore nonexistent files and arguments and never prompt before deletion
-i Use the -i option to prompt before every removal or a file
-r, -R, –recursive Use the -r or -R or –recursive to remove directories and their contents recursively
-d, –dir Use the -d or –dir to remove empty directories
–no-preserve-root Use the –no-preserve-root to not treat ‘/’ specially. By default, the / directory is not removed
–help Display a help message and exit.

Examples:

Below are some examples of how to run and use the rm on Ubuntu Linux.

Simply run the rm to invoke it.

If you want to use the rm command to remove a file named Private.txt , you simply run the commands below:

rm Private.txt

When you run the above command and you have rights to delete from that directory, the command will delete the file defined above. ( Private.txt )

If you don’t have permission to delete content from the parent directory (directory the file lives), you won’t be able to. You’ll will get “Operating not permitted” error. This simply means you don’t have rights there to carry out the operation you executed.

Each time you run the rm command to delete an item, you’ll get prompted to confirm that you want to delete the specified file. If you prefer not to be bother or prompted, you use the -f or –force option.

rm -f Private.txt

The -f option tells rm never to prompt the user and to ignore nonexistent files and arguments when running.

To use the rm command to delete one or more empty directories, you use the -d option.

rm -d PrivateDirectory

rm -d behaves exactly as rmdir command wrote about previously.

However, the rm -d command only delete empty directories. If the directories contain files and other directories, the command will fail.

To remove all non-empty directories and all files within them recursively, you run the rm command with the -r option against the directories.

rm -r PrivateDirectories

To force-ably remove non-empty directories and all content in them without being prompted, use the -r and -f option:

rm -rf PrivateDirectories

If you don’t have rights, you may have to use the rm command with sudo.

sudo rm -rf PrivateDirectories

Congratulations! You have learned how to delete or remove content using the rm command on Ubuntu

You may also like the post below:

Published by Richard

In my spare time, I research topics that are interesting and worthwhile for users and students who want to try something new. I, too, am a student and my focus here is to help other students and new users get started with managing Ubuntu Linux, Windows, Content Management Systems (CMS) and others.

I try to do my best explaining the topics and detailing the instructions so that anyone can understand. These tutorials may not work in all situations and for all users. However, if you run into trouble, please ask your questions below and I or someone from the community may help you resolve. Thanks for reading and hope you come back.

2 Comments

It will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Ubuntu Linux.

Type the ls > output.txt command to print the output of the preceding command into an output.txt . On Linux, we can use the fsck command to find and fix file system errors. It uses the following syntax: mv [options] [source] [destination] Most commands demonstrated in this article work with other implementations of find, but should you try a command on a platform other than Linux and get unexpected results, try downloading and installing the GNU version. Deleting files in Linux couldn’t be easier. Use find command to find a file (or files) using file extensions. $ file etc Find File Type in Linux 2. The Linux file command displays the type of a file. Let’s create some directories. ‘ASCII text’) or MIME type (e.g. There are in fact a couple of more commands that shows you specific information of a file depending on the type of the file. 3. find . H ow do I determine the file type under UNIX or Linux like operating systems? Whether you use the file manager or work directly in the terminal with the command “rm”, you can remove Linux files in just a few clicks. Limit a search by file type. The xdg-mime command is a member of the xdg-utils package from freedesktop.org. The command doesn’t take the file extension into account, and instead runs a series of tests to discover the type of file data. An example of an absolute path is /home/username. Before that I would like to know what my current file system type is for various mount points I have on my UNIX system. Put the name of the file in Terminal (the name of the file with an appended space) then type “file”. file command is used to determine the type of a file. To achieve this it analyzes the header, magic number and the contents of the file. chown − The chown command stands for “change owner” and is used to change the owner of a file. ‘text/plain; charset=us-ascii’). file * Used to list types of all the files. If the file doesn’t have an extension then in Linux we can use file utility. find . If we want to delete files of the same or different extensions from our Linux system, we must follow many different types of commands. (By reading the file header). The command then writes the file types to standard output. It has a simple syntax with only a few options: file [option] filename Now that you know the syntax, let’s see how to use file command. This command tests each argument in an attempt to categorize it. -type f -name filename.txt. Again, mount command will also refer /etc/mtab file to get the list of mounted file systems and can also help you determine file system type of individual devices. find ./ -name “*.page” -type f -print0 : The find action will start in the current directory, searching by name for files that match the “*.page” search string. While working on the Linux command line, there may be times when you’d encounter a file which is an archive (say a .zip file), but its extension (or lack of it) would suggest otherwise. It lists all the files with a format extension of .png. $ ls. 10. mount command. Now /proc/mounts file refers /etc/mtab so this is not a new method but just another file which you can look into to check file system type in Linux or Unix. ‘ASCII text’) or MIME type (e.g. Also different types of file systems are supported by different Linux Kernel systems. You can use the command for concatenating and printing standard file output. Here, ‘ filename1 ‘, ‘ filename2 ‘, etc. Use the command line to navigate to the Desktop, and then type cat myFile. -type f -exec bash -c ‘ [ [ “$ ( file -bi “$1″ )” == */x-shellscript* ]]’ bash <> \; -print. Use the file command to read the files specified by the File or -fFileList parameter, perform a series of tests on each one, and attempt to classify the files by type. To view the contents of a file using cat, simply type the command name followed by the file you want to view. 4. touch Command. # Make a second copy of a file in the same directory cp myspecialfile.one myspecialfile.two # Copy a file to a folder named newlocation in the home directory cp myfile

/newlocation/ # Copy a directory including all files and subdirectories to your home directory 1. Like any other Linux command, you can use. -type f -name myfile This command will run a search in the current directory and its subdirectories to find a file (not directory) named myfile. How to Mount and Unmount Filesystem / Partition in Linux(Mount/Umount Command Examples) Mount a CD-ROM. Similarly, / for /root and

Delete and remove files on ubuntu linux using terminal. In this tutorial, you will learn how to delete and remove a file on Ubuntu Linux based system using terminal or command prompt.

This tutorial will use the rm command. It tries to remove the files specified on the command line. Use the rm command to delete files and directories on Ubuntu Linux. This tutorial will guide you on how to delete and remove files on Ubuntu Linux with a terminal using rm command.

The rm command removes files on Ubuntu Linux. The rm command options are as follows:

  • -f : Remove read-only files immediately without any confirmation.
  • -i : Prompts end-users for confirmation before deleting every file.
  • -v : Shows the file names on the screen as they are being processed/removed from the filesystem.
  • -R OR -r : Removes the given directory along with its sub-directory/folders and all files.
  • -I : Prompts users everytime when an attempt is made to delete for than three files at a time. Also works when deleting files recursively.

This tutorial will explain all the options for the rm command one by one below.

Commands to delete and remove files on Ubuntu Linux

Just follow the rm and unlink command to remove files on Ubuntu Linux using terminal:

  1. Open the Ubuntu terminal
  2. Type any one of the following command to delete a file named hello.txt in the current directory
  3. rm hello.txt
    OR
    unlink hello.txt

WARNING: Do not type sudo rm -R / or sudo rm -r / or sudo rm -f /* or sudo rm –no-preserve-root -rf / as it removes all the data in the root directory. Avoid data loss and you should not execute them!

Command to delete multiple files on Ubuntu Linux

Use the following command to delete the file named hello.txt, my.txt, and abc.jpg placed in the current directory:

You can specify path too. If a file named hello.txt placed in /tmp/ directory, you can run:

To delete a file and prompt before every removal in Ubuntu Linux

To get confirmation before attempting to remove each file pass the -i option to the rm command on Ubuntu Linux:

Force rm command on Ubuntu Linux to explain what is being done with file

Pass the -v option as follows to get verbose output on Ubuntu Linux box:

To delete all files in folder or directory in Ubuntu Linux

Use the following command with following options to delete all files in folder or directory in Ubuntu Linux:

The above given commands will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Ubuntu Linux.

Ubuntu Linux delete file begins with a dash or hyphen

If the name of a file or directory or folder starts with a dash ( – or hyphen — ), use the following syntax:

Do not run ‘rm -rf /‘ command as an administrator/root or normal Ubuntu Linux user

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Ubuntu Linux disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands on Ubuntu Linux:

Conclusion

Delete and remove files on ubuntu linux using terminal. In this tutorial, you have learned how to delete and remove a file on Ubuntu Linux based system using terminal or command prompt.

How to remove multiple subdirectories with one linux command

In this tutorial, we are going to learn how to use rm command to remove all files safely from a directory. This document helps you delete non-hidden files, files with specific extensions, hidden files inside a directory.

01. To delete all non-hidden files from a directory, type:

02. To remove all the file with the extension .txt from a directory, type:

03. To delete all non-hidden files and sub-directories along with all of their contents from a directory, run:

04. To delete all hidden files and directories from a folder, type:

05. To delete all the files from inside a folder but not removing its sub-directories:

06. To remove a folder whose name has space, make sure to always use quotes like:

You can also use backslack to remove spaces by escaping the space.

To remove the directory named ‘Good Morning’, type:

07. You can see what is being done when deleting all files in directory pass the -v option to the rm command:

08. To remove all the file from a directory having extension .sh you can use find command too,

Note: In place of “*.sh” just give “*” to delete all the files.

Understanding rm command option

rm : Remove (unlink) the FILE(s).
-f : ignore nonexistent files and arguments, never prompt
-r : remove directories and their contents recursively
-v: see what is happening

Conclusion

You need to be careful while removing the file on the Linux system. Using the command ‘rm’ will not store files in the trash. On the other hand, be careful while using wildcard like ‘*’.

In this post I will show you couple of ways to bulk delete files with a specific extension in a folder and all its subfolders. This can be useful, for instance, if a virus infected your PC and automatically created .exe files in all folders, or if you want to delete automatically created .log or .tmp files. You won’t mind manually deleting if there are only a few files/folders, but what if there are hundreds of such unwanted files stored in as many folders? We can avoid the pain by using a simple Command Prompt command. Or if you aren’t comfortable with command line usage, scroll a bit further down for a software to achieve the same result.

Delete Files of Specific Extension using Command Prompt

Open Command Prompt by entering CMD in the Run dialog or by searching for it in the Start menu/screen.
Switch to the folder in which you want to perform the deletion operation. Once there, type in the following command (assuming to delete .tmp files):

This command will delete all the ‘Tmp’ files from the folder you are in, and all of the subfolders.

Here,
/S : Instructs to delete files from all subdirectories.
/Q : Deletes files quietly, i.e., without prompts.

You can also specify multiple file extensions in the command like this:

To see more information on the command, type in DEL /? in the prompt window.

Delete Files of Specific Extension with Folder Cleaner

Folder Cleaner is a free software which allows users to delete files from multiple folders on your computer and from network devices in batches. It functions using file filters – this lets you include and exclude files based on its extension.

Before starting the deletion process, the program allow you to pre-analyze the chosen search criteria. So, if any unwanted result is found, you can correct it before actually executing the task. Another useful option is the possibility to delete files to Recycle Bin, which is not possible in the command line method.

Overall, the program is simple and quite easy to use. Just specify what you want to do and where, and you’re good to go. It’s portable, so you can even carry it on your portable drive.

Folder Cleaner works on both 32-bit and 64-bit editions of Windows 8, Windows 7, Windows Vista, and Windows XP.

6. Find all the files except the ones under the temp directory. Also print the temp directories if present:
The only difference here is the print statement being present in the first half as well.

7. Find all the .c files except the ones present in the C directory:
The 1st part prunes out the C directories. In the second part, all the .c files are found and printed except the ones present in C.

8. Find all the .c files except the ones present in the C and temp directory:
To specify multiple directories with the -name option, -o should be used as an OR condition.

9. Find all files modified in the last one day except the ones present in the temp directory:
Usage of mtime makes find to search for files modified in the last day alone.

10. Find only regular files modified in the last one day except the ones present in the temp directory:
Using the -type f option, find will find only the regular files alone.

11. Find all files whose permission is 644 except the ones present in the temp directory:
-perm option in find allows to find files with specific permissions. permission 644 indicates files with permission rw-r–r–.

12. Same using the wholename option and prune to exclude directory:

13. Using exec and prune to exclude directory in-place of name:
One more way. Using the exec, a condition can be put to check whether the current file is “temp”. If so, prune it. ‘<>‘ is the file found by the find command.

14. Using inum and prune to exclude directory in-place of name option:
Same, but using the inode number of the temp directory. inode number is specified using the inum option.

15. Find the list of all .c files except the ones present in the C directory without using prune:
-path option is like the -wholename option. It is used to specify a specific path to search. In this case, ‘! -path ” tells to exclude this path alone. Since the specific path has been excluded in this way, the prune is not needed at all.