- recursively delete subdirectories and files [duplicate]
- 3 Answers 3
- Delete Files Recursively in Linux
- Use the rm Command to Delete Files in Linux
- Use the -r Command to Delete Files Recursively in Linux
- Use Wildcard * to Delete Files With Similar Filenames in Linux
- Use Wildcard * to Delete Files With Same Extension in Linux
- Use the find Command to Delete Files Recursively in Linux
- Related Article — Linux File
- How to Remove Files Recursively in Linux
- Deleting all files recursively in Linux
- How to remove files recursively by size
- How to remove files recursively by extension (File type)
- How to delete files recursively based on modification or creation time
- Conclusion
- About the author
- David Adams
- Bash script to recursively step through folders and delete files
- 6 Answers 6
recursively delete subdirectories and files [duplicate]
Is there a command that I can use from the Parent_dir to recursively remove all the delme_dir directories and the delme_file files ?
3 Answers 3
In single command you could do this:
find . \( -type d -name "delme_dir" -o -type f -name "delme_file" \) -delete
If there is no other directories or files match with pattern delme_* , you can do in short.
You can combine find , xargs and rm :
find . -type f -a -name delme_files -print0 | xargs -0 rm
find . -type d -a -name delme_dir -print0 | xargs -0 rmdir
(when you are in the Parent_dir directory)
There is probably a way to combine the two find into only one but I find it clearer like that anyway.
The find commands will find the directories/files named per your example, and you pipe the results to xargs that will execute rm (remove) / rmdir (remove directory) on each of the file find «upstream», that is from the file command.
If the directories are not empty, replace rmdir by rm -Rf but this will remove recursively all files in the directories found by the find command.
The combo -print0 / -0 will make sure that all files/directories will be dealt correctly, even those having spaces in their names (maybe not needed here but better to use in generic cases).
Make sure to test things before on non important data and always have backups.
Delete Files Recursively in Linux
This article explains how to delete files in Linux. Then, we will elaborate on the topics below.
- Delete files recursively.
- Delete files with the same extension.
- Delete files with similar filenames.
- Delete files recursively with the same extension / similar filenames.
The sample files and directories we will use throughout the article are below.
Use the rm Command to Delete Files in Linux
After the rm command, type the filename(s) you want to remove.
Use the -r Command to Delete Files Recursively in Linux
The -r flag allows you to recursively remove directories and their contents. Type the directory name you want to delete after the rm -r command. The use of a slash / after the directory name is optional.
Use Wildcard * to Delete Files With Similar Filenames in Linux
The asterisk * is called wildcard, and it gives every file that starts with the specified name as a parameter to the rm command.
We want to remove all files with the name file1 , even if the extension is different. We use a wildcard instead of specifying the extension at the end of the filename.
Use Wildcard * to Delete Files With Same Extension in Linux
This time, we want to remove all files with the same extension, even if their names are different. We use a wildcard instead of the filename then write the extension.
Use the find Command to Delete Files Recursively in Linux
We can use the find command to find and delete files recursively with similar extensions or filenames from a directory and its sub-directories.
We can use the find command with the -delete .
find . -type f -name '*.txt' -delete
Alternatively, it can be used with the exec .
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
Related Article — Linux File
Copyright © 2023. All right reserved
How to Remove Files Recursively in Linux
After reading this article, you will be able to find and remove single or multiple files from the command line. This tutorial is optimized for both new and experienced Linux users.
The first section of this tutorial explains how to remove files recursively (directories with all their content and subdirectories’ content). Below I also added instructions to remove recursively certain types of files depending on their size, extension, creation or modification time, and permissions.
All practical examples in this document contain screenshots to make it easy for every Linux user to understand and apply them to their needs.
Deleting all files recursively in Linux
The first section shows how to use the rm (Remove) command to delete a directory with all its content, including all subdirectories with their files and additional subdirectories.
The rm command used with -r flag will remove all directories’ content independently of their type.
But first, let’s see the directories in my home using the ls command.
As you can see, I have 5 directories: Desktop, dir2, Documents, Downloads, and removerecurdir.
Let’s see the content of the directory named removerecurdir using the command tree as shown in the screenshot below.
According to the tree output the removerecurdir directory contains two directories that contain subdirectories and a file inside removecurdir: The directory dir1, with otherdir and otherdir2 subdirectories, and the directory dir2 contains a file named file3.
Let’s say we want to remove the removecurdir and all its content including all files and subdirectories. The proper command is the rm command followed by the -r flag as shown in the syntax below.
Thus, if I want to remove the removerecurdir with all the content, I run:
The subsequent ls output shows the directory, and all its content were successfully removed.
How to remove files recursively by size
This section shows how to recursively delete files smaller than 10 megabytes using the command find.
The syntax is the following:
Note that in the example below, I use sudo to get privileges to remove protected files.
The syntax to remove files greater than a specific size is very similar. The minus (-) symbol must be replaced by a plus symbol (+). The exact syntax is shown below.
In the example below I will use the previous syntax to remove files greater than 1 GB.
How to remove files recursively by extension (File type)
The current chapter explains how to delete files recursively by extension or file type.
On my home I have a directory named testhint. Let’s see its content using the tree command.
As you can see, the parent directory testhint contains a file (file1.txt) and two subdirectories: testhint2 containing file3.txt and the testhint3 subdirectory containing file3 and something.txt.
Let’s assume you want to recursively remove all txt files only. The syntax is the following:
Thus, to remove all txt files recursively within the parent directory testhint, I run the command shown in the figure below.
As you can see all txt files were removed, and only file3 without an extension remains.
You can also delete files by extension using find together with exec commands, as I will explain below.
Let’s see a new scenario with the same directory structure but different files.
The above image shows 4 log files and 3 files without extension.
The syntax to remove files by extensions using -exec is the following:
Thus, to remove the .log files from the previous screenshot, I ran the command below.
The image above shows all .log files were deleted while other files remained.
The xargs command offers the same solution. The difference between xargs and exec is that exec runs the rm function every time a file matches the condition. The command xargs executes the rm command once for all found files matching the condition.
The syntax to remove all files by extension with find and xargs is the following:
The new scenario depicted in the screenshot below shows five .c files in different subdirectories and five files without the .c extension.
To remove all .c files using xargs I run the command as shown below.
Again, you can see the selected extension files were successfully deleted.
Deleting all files recursively based on permissions
Let’s check the new content of the testhint directory.
There are four files with full permissions (file2, file3.c, file6.c and file7).
Now let’s assume you want to find and remove all files with full permissions for everyone.
The syntax is the following:
Thus, to remove all files with full access to all users, I execute the command below.
How to delete files recursively based on modification or creation time
The last section of this tutorial explains how to delete files recursively by creation or modification time.
The syntax is the following:
If you want to delete files created or modified in the last day (last 24 hours), run the following command, where 1 is the number of days, and the minus (-) symbol specifies files created or modified before the defined number of days.
To remove files created or modified before a day, before 24 hours, just replace the minus symbol for a plus symbol.
Conclusion
Since Linux is a very versatile and flexible operating system, users have different techniques to get the same result. All the alternatives explained above are valid for almost every Linux distribution. Some of the commands are even useful for some Unix systems. As you can see, implementing them is easy and any Linux user can do it independently of their knowledge level. To delete files recursively according to other conditions, check the main page of each command described in this article.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
Bash script to recursively step through folders and delete files
Can anyone give me a bash script or one line command i can run on linux to recursively go through each folder from the current folder and delete all files or directories starting with ‘._’?
Just FYI: the ._ files are called AppleDouble files. On Mac systems, files have a data fork and a resource fork. The resource fork typically holds information such as icons, the file’s spatial position in the folder (in Finder), and other metadata. The data fork (the actual file) contains the actual important data, so discarding the AppleDouble file shouldn’t be problematic.
6 Answers 6
Change directory to the root directory you want (or change . to the directory) and execute:
find . -name "._*" -print0 | xargs -0 rm -rf
xargs allows you to pass several parameters to a single command, so it will be faster than using the find -exec syntax. Also, you can run this once without the | to view the files it will delete, make sure it is safe.
This will be confused by filenames with spaces, which are common if you’re dealing with files from a Mac environment. Use the null-delimiter options to find and xargs ( find . -name «._*» -print0 | xargs -0 rm -rf ) to avoid this problem.
I’ve had a similar problem a while ago (I assume you are trying to clean up a drive that was connected to a Mac which saves a lot of these files), so I wrote a simple python script which deletes these and other useless files; maybe it will be useful to you:
Yep, u assumed correctly. Supposedly its something apple are looking to fix to make macs more ‘network friendly’.
find /path -name "._*" -exec rm -fr "<>" +;
Instead of deleting the AppleDouble files, you could merge them with the corresponding files. You can use dot_clean .
dot_clean — Merge ._* files with corresponding native files.
For each dir, dot_clean recursively merges all ._* files with their corresponding native files according to the rules specified with the given arguments. By default, if there is an attribute on the native file that is also present in the ._ file, the most recent attribute will be used.
If no operands are given, a usage message is output. If more than one directory is given, directories are merged in the order in which they are specified.
Because dot_clean works recursively by default, use:
If you want to turn off the recursively merge, use -f for flat merge.