run_multiple_commands_with_find

2026-10-30 1 min read Bash Linux Networking

How to Run Multiple Commands with -exec in find Using a Script

1. Create the Script a

Save the following as a (make sure it’s executable), or any other name of your choice:

1
2
3
4
5
6
7
8
#!/bin/bash
# $1 is the file path passed from find

echo "Processing: $1"
# Example: print file size
stat --format="Size: %s bytes" "$1"
# Example: display first line
head -n 1 "$1"

Make it executable:

1
chmod +x ./a

Validation: This script prints the filename, its size, and the first line.

2. Use find with -exec ./a

Run:

1
find . -type f -exec ./a {} \;

Validation: This command finds files, then runs ./a <filename> for each file. All commands inside a run sequentially for each file.

Summary

  • Bundle multiple actions in an executable script.
  • Pass each found file as an argument to your script using -exec.
  • This method keeps find usage clean and scales to complex multi-step logic.
comments powered by Disqus