- Linux Delete Folder Recursively Command
- rm command syntax to delete directories recursively
- Examples that examples how to delete folder recursively
- Removing folders with names containing strange characters
- Deleting folder recursively command summary
- How to Search and Remove Directories Recursively on Linux
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How to find and delete directory recursively on Linux or Unix-like system
- Find command syntax to delete directory recursively
- Finding and deleting directory recursively using xargs
- Shell script to recursively remove backups older than 30 days
- Remove Directory Recursively without Prompting for Confirmation in Linux
- Step 1: List Contents of Directories
- Step 2: Remove a Single Directory Recursively without Prompting the User for Confirmation
- Step 3: Remove Multiple Directories Recursively without Prompting the User for Confirmation
- Step 4: Verify Deletion of Specified Directories
- Conclusion
- About the author
- Aqsa Yasin
Linux Delete Folder Recursively Command
rm command syntax to delete directories recursively
The syntax is as follows:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | 2m |
rm -r dirName
## OR ##
rm -r folderName
## OR ##
rm -rf folderName
Did you know?
Everything is a file in Linux and Unix-like systems. In other words, your pictures, documents, directories/folders, SSD/hard-drives, NIC, USB devices, keyboards, printers, and some network communications all are files.
Examples that examples how to delete folder recursively
In this example, recursively delete data folder in the current home directory:
The specified /home/vivek/data/ will first be emptied of any subdirectories including their subdirectories and files and then data directory removed. The user is prompted for removal of any write-protected files in the directories unless the -f (force) option is given on command line:
To remove a folder whose name starts with a — , for example ‘ —dsaatia ‘, use one of these commands:
We can add the -v option to see verbose outputs. In other words, the rm command will explain what is being done to our files and folders on Linux. For instance:
rm -rfv /path/to/dir1
rm -r -f -v /home/vivek/oldpartpics
Removing folders with names containing strange characters
Your folders and files may have while spaces, semicolons, backslashes and other chracters in Linux. For example:
ls -l
Let us say we have a folder named “ Our Sales Data ” and “ baddir# ” or “ dir2 ;# “. So how do we delete those directories with special names containing strange characters? The answer is simple. We try to enclose our troublesome filename or folder name in quotes. For example:
rm ‘Our Sales Data’
rm -rfv ‘/path/to/Dir 1 ;’
rm -r -f -v «baddir#»
rm a\ long \dir1 \name
Sometimes, we need insert a backslash ( \ ) before the meta-character in your filename or folder name:
rm \$dir1
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Deleting folder recursively command summary
Command and options | Description |
---|---|
-f | Forceful option. Ignore nonexistent files and arguments, never prompt |
-r | remove directories and their contents recursively |
-v | Verbose output |
rm — ‘-dir1’ | Remove a dir/file whoes name start with a ‘ — ‘ |
rm ./-dir1 | Same as above |
rm -rfv ‘dir name here’ | Enclose your troublesome filename/folder in quotes |
rm -rfv \$dirname1 | Same as above |
See Linux rm(1) command man page or rm command example page for more information:
man rm
rm —help
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to Search and Remove Directories Recursively on Linux
In one of our previous articles, we explained how to find out top directories and files consuming the most disk space on file system in Linux. If you notice that such directories no longer contain important files and subdirectories (such as old backups, downloads etc..), then you can delete them to free up space on your disk.
This short tutorial describes how to find and delete directories recursively in the Linux file system.
To achieve the above purpose, you can employ the find command together with rm command using the syntax below. Here, the + sign at the end enables multiple directories to be read simultaneously.
Attention: You must use rm command carefully because it is one of the most dangerous commands to use in Linux: you may accidentally delete critical system directories, thus resulting to system failure.
In the example below, we will search for a directory called files_2008 and delete it recursively:
You can also use find and xargs; in the following syntax, -print0 action enables printing of the full directory path on the standard output, followed by a null character:
Using the same example above, we have:
Last but not least, if you are concerned about the security of your data, then you may want to learn 3 ways of permanently and securely deleting ‘Files and Directories’ in Linux.
Do not forget to read more useful articles about file and directory management in Linux:
In this article, we showed you how to find and remove directories recursively on Linux. If you have any question or extra ideas you want to add to this topic, use the comment section below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
How to find and delete directory recursively on Linux or Unix-like system
Find command syntax to delete directory recursively
Try the find command:
find /dir/to/search/ -type d -name «dirName» -exec rm -rf <> +
Another option is as follows to recursively remove folders on Linux or Unix:
find /dir/to/search/ -type d -name «dirName» -exec rm -rf \;
Warning : Be careful with the rm command when using with find. You may end up deleting unwanted data.
Find will execute given command when it finds files or dirs. For example:
find . -type d -name «foo» -exec rm -rf <> +
OR
find . -type d -name «bar» -exec rm -rf «<>» \;
Sample outputs:
You can find directories that are at least four levels deep in the working directory /backups/:
find /backups/ -type d -name «bar» -depth +4 -print0 -exec rm -rf <> +
Finding and deleting directory recursively using xargs
The syntax is as follows to find and delete directories on Linux/Unix system. For example, delete all empty directories:
find /path/to/dir/ -type d -empty -print0 | xargs -0 -I <> /bin/rm -rf «<>»
In this example, remove all foo directories including sub-diretories in /backups/ folder:
find /backups/ -type d -name «foo*» -print0 | xargs -0 -I <> /bin/rm -rf «<>»
The second command is a secure version. It is fast too, and it deals with weird directory names with white spaces and special characters in it:
Hence, I would like readers to use the following syntax:
find /path/to/search/ -name «pattern» -print0 | xargs -0 -I <> /bin/rm -rf «<>»
Where find command options are:
- -name «pattern» : Base of file name that matches shell pattern pattern. For example, foo, Foo*3 and so on.
- -print0 : Show the full file name on the standard output, followed by a null character. This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs
And xargs command options are:
- -0 : Input items given by find are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
- -I <> : <> in the initial-arguments with names read from standard input. For example, dir names given by find command./li>
- /bin/rm -rf «<>« : Run rm command that remove files or directories passed by <> .
Shell script to recursively remove backups older than 30 days
Here is my sample script that removes older weekly backup of my VM tarballs stored in the following format:
Источник
Remove Directory Recursively without Prompting for Confirmation in Linux
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.
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
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.
Источник