bash:find:run_one_or_more_commands_for_each_of_finds_results
Table of Contents
BASH - Find - Run one or more commands for each of finds results
To run a single command for each file found:
find dirname ... -exec somecommand {} \;
To run multiple commands in sequence for each file found, where the second command should only be run if the first command succeeds:
find dirname ... -exec somecommand {} \; -exec someothercommand {} \;
To run a single command on multiple files at once:
find dirname ... -exec somecommand {} +
Example usage of running multiple commands against each file returned by find
find . -type f -name "*.mp3" -print0 | while IFS= read -r -d '' file; do echo "$file"; [[ $(mp3info -r m -p "%r" "$file") -gt 192 ]] && lame -b 192 "$file" "${file%.*}".192.mp3 && mv "${file%.*}".192.mp3 "$file"; done
NOTE: This searches for mp3 files, then checks if their bitrate is greater than 192 kbps; and if so then reduces the bitrate to 192 kbps.
- IFS=: Usually the read command removes all leading and trailing whitespace characters. This is not wanted here, so this clears this whitespace action.
- The parameters used with read are:
- -r: Do not treat a backslash as an escape character.
- -d '': Sets the delimiter of the line; i.e. what character is used to terminate the line. Here, the entire line is used.
- This uses the mp3info command to determine the bitrate.
- This mp3info command needs to be installed with sudo apt install mp3info.
References
bash/find/run_one_or_more_commands_for_each_of_finds_results.txt · Last modified: 2022/06/13 08:33 by peter