Published: 2026-11-29 16:37 Updated: 2026-11-29 17:17 By Žan Šadl Ferš In Scripting .
tags: Bash scripting Recursive file processing find
While trying to batch delete a bunch of unneeded junk files that just happed to be grouped with other files i need, i got sick of manually cleaning up using my file manager and decided to script that away with the help of find .
The Blog posts of Dejan Panovski for the find command also helped out in making this script.
#!/usr/bin/env bash
PATTERN = " ${ 1 } "
ROOT_DIRECTORY = " ${ 2 :-$( pwd ) } "
find " ${ ROOT_DIRECTORY } " -type f -name " ${ PATTERN } " -delete
FAIR WARNING! find by default works recursively, and if you carelessly set the ROOT_DIRECTORY or botch the script in any other way, you could lose your data , so be careful! I am not responsible for any damage happening to your computer/s for misuse of this script!
Now, back to what the script does exactly: find recursively looks for files matching PATTERN (using glob pattern if needed), and if any match, these get deleted immediately .
The 2. positional argument can be overriden if the deleting needs to happen in a specific directory.
Running the script Example showing how to delete a bunch of generated .csv files that are no longer needed in the current working directory:
./delete_files_recursively.sh 'chart[0-9].csv'
Copyright notice: © 2026 Žan Šadl Ferš All rights reserved. All original songs, stories, texts and other works on this website are protected by copyright. Copying, reproduction or use without the author’s prior permission is not permitted, except in cases permitted by Slovenian law.