Delete Files Recursively by Pattern
Find and delete files matching a specific pattern recursively through directories using the find command
Delete Files Recursively by Pattern
Overview
Find and delete files matching a specific pattern recursively through directories using the find command
Prerequisites
- Terminal access
- Bash knowledge
Instructions
If your current directory is ~/dir1/ and you want to find all files that are called something.bak,
even if they are in ~/dir1/dir2/dir3/, then you should run this command:
find . -name "*.bak" -type f
I took for granted that you all understand that with
something.bakI mean any file with the extension.bak:)
Now that you checked all the files that are called like that, you can delete them all with adding the option -delete, just this way:
find . -name "*.bak" -type f -delete
Note: I recommend to use this command with precaution. With the first command you check the files you are about to delete. Also, make sure that
-deleteis the last argument in your command. If you put it before-name *.bak, it will delete everything.
Troubleshooting
If you encounter any issues:
- Verify all prerequisites are installed
- Check command syntax carefully
- Ensure you have necessary permissions
Related Utilities
- Column Removal