I want to run:
./my_script.py a_file &
... on all files in the current folder that end with .my_format
, so I do:
find . -type f -name "*.my_format" -exec ./my_script {} & \;
but it doesn't work. How should I include &
in the -exec
parameter?
"&&" is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors (or, more accurately, exits with a return code of 0).
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.
The Bash find command in Linux offers many ways to search for files and directories. In this tutorial, you'll learn how a file's last-time access, permissions, and more can help find files and directories with the Bash find command.
$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails. $@
Try this:
$ find . -type f -name "*.my_format" -exec sh -c './my_script {} &' \;
The mostly likely reason your attempt didn't work is because find executes the command using one of the exec(3)
family of standard c library calls which don't understand job control - the &
symbol. The shell does understand the "run this command in the background" hence the -exec sh ...
invocation
Try this find command:
find . -type f -name "*.my_format" -exec bash -c './my_script "$1" &' - '{}' \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With