While trying to batch convert alot of video and audio files, i got sick of manually creating custom commands for that (and other related things) and then running them manually per terminal instance, so i decided to create something more modular, which ended up being a Bash script using the good old find.
The Blog posts of Dejan Panovski for the find command also helped out in making this script.
#!/usr/bin/env bash
ROOT_DIRECTORY="${1}"
PATTERN="${2}"
# For getting the matched filepath, use {} as part of the ${@:3} arguments slice!
find "${ROOT_DIRECTORY}" -type f -name "${PATTERN}" -exec ${@:3} \;
It searches for whatever matches PATTERN (using glob pattern if needed), and for each matched file, a command (obtained from ${@:3}) runs. find by default searches files recursively, so keep that in mind. This can be overriden adding the argument -maxdepth 0 to the find command.
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 or worse, so be careful! I am not responsible for any damage happening to your computer/s for misuse of this script!
While it would be better to have ROOT_DIRECTORY point towards the current working directory by default (one less thing to write in the terminal, when not needed), that is impossible due to ${@:3} with the current way the script is written. The arguments are all positional, and no matter if ROOT_DIRECTORY is the 1. or 2. argument by position, the command to execute and its arguments need to be set afterwards, no matter what. So, the way it way done is a compromise, that can possibly be done better in the future.
Running the script
Example showing unzipping a bunch of zip files in their current locations recursively:
./exec_cmd_on_files_recursively.sh ./ '*.zip' unzip {}