Interview Prep/Shell Scripting/find, xargs, jq and yq

What is the difference between `-exec cmd {} \;`, `-exec cmd {} +` and `-delete`?

IntermediateConceptfind, xargs, jq and yq
Answer
  • -exec cmd {} \; runs cmd once per file: slow for thousands of files.
  • -exec cmd {} + passes as many files as fit into one command line, like xargs: fast.
  • -delete removes files internally with no process at all, and implies -depth.
find /var/log/app -name '*.log' -mtime +7 -exec gzip -- {} +
find /var/log/app -name '*.gz' -mtime +30 -delete
find /srv/app/releases -mindepth 1 -maxdepth 1 -type d -mtime +10 -exec rm -rf -- {} +

Put -delete last: find . -delete -name '*.tmp' deletes everything before the name test is applied.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on find, xargs, jq and yq