There are several ways, each more catered towards a specific need. Welcome to the UNIX world.
This runs the commands through nohup
:
find . -type f -name 'abc' | xargs -I '{}' nohup '{}' -p
Explanation:
find .
look in the current directory and it subdirectories.
-type f
look for a file.
-name 'abc'
the filename should be exactly "abc" (case-sensitive).
|
pipe the resulting list of files to another program.
xargs
send the individual entries to another program.
-I '{}'
in the following expression, substitute '{}'
with the entry.
nohup
this is the program that xargs
will invoke multiple times, each time passing one of the find
results as a parameter. This program is used to launch jobs that will run in the background and will not be interrupted if the current session disconnects. To let you check the results, the output of commands executed through nohup
is by default appended to the file nohup.out
in the current directory.
'{}'
this is a placeholder and will be substituted with the find
results.
-p
this will be appended as an additional parameter.
Example: let's say you have one file called "abc" in the current folder, and another in a subfolder called "sub"; the commands that will be executed are:
nohup ./abc -p
nohup ./sub/abc -p
Note: it could be useful to run xargs -p
as it will display each single command before executing it. Answering y
will actually launch it, n
will skip it (find . -type f -name 'abc' | xargs -p -I '{}' nohup '{}' -p
).
Or using sudo
:
find . -type f -name 'abc' | xargs -I '{}' sudo -u $USER '{}' -p
sudo
executes a command and allows to choose the user it has to be run as. In this example, it uses the current user, whose name is found in the $USER
environment variable.
Or using bash
(or your favorite shell):
find . -type f -name 'abc' | xargs -p -I '{}' bash -c "'{}' -p"
Here the commands are passed to bash, but you can choose to run them with a different shell (if you have it installed, of course). Just substitute bash
with your choice of sh
, csh
, tcsh
, ksh
, zsh
...
Or using parallel
:
find . -type f -name 'abc' | parallel --no-notice -I '{}' "'{}' -p"
parallel
is usually not installed by default but it can be worth using; think of it as an enhanced xargs
(it's written to work with the same options) that has the useful feature of being able to run the entries it receives instead of always needing to pass them to another program (at least, I wasn't able to get xargs to do that). As its name suggests, commands are by default run in parallel maximizing cpu/thread utilization. The --no-notice
option hides the citation it always diplays until run with --bibtex
.
Or with at
:
find . -type f -name 'abc' | xargs -I '{}' echo '{}' -p | at now
at
schedules a command to be run at the specified time; in this example now
is used to run the commands immediately.
-shutdown 0
, wait 10-20 years, and try again.