Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: -exec in find command and &

Tags:

find

bash

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?

like image 773
Ricky Robinson Avatar asked Nov 22 '13 14:11

Ricky Robinson


People also ask

What does && do in bash?

"&&" 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).

What is $@ in bash?

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.

Is find a bash command?

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.

What does $_ mean in bash?

$_ (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. $@


2 Answers

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

like image 129
holygeek Avatar answered Oct 23 '22 20:10

holygeek


Try this find command:

find . -type f -name "*.my_format" -exec bash -c './my_script "$1" &' - '{}' \;
like image 25
anubhava Avatar answered Oct 23 '22 22:10

anubhava