3

How do I manage to find a program in a directory and subdirectories, then run the programs?

For example,

find . -type f -name "abc" 

finds all abc executables and:

find . -type f -name "abc" | RUN "abc" -p (WRONG!!!)

runs the abc programs with one parameter "-p".

How can I achieve it?

1
  • or just do -shutdown 0, wait 10-20 years, and try again.
    – Andrew
    Jun 25, 2019 at 15:08

3 Answers 3

2

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.

4
  • Could you give some explanation on the first command line: "find . -type f -name 'abc' | xargs -I '{}' nohup '{}' -p" ?
    – Matt Elson
    Mar 8, 2017 at 11:47
  • the first command line returns the result "nohup: appending output to `nohup.out'"
    – Matt Elson
    Mar 8, 2017 at 11:51
  • @MattElson The solution here runs all commands through nohup. That means all output are redirected to the file nohup.out. For a direct output on screen, please consider the commands I listed.
    – xuhdev
    Mar 8, 2017 at 18:24
  • nohup is just one of the solutions: sudo outputs to the terminal, as do bash and parallel which I added later.
    – simlev
    Mar 9, 2017 at 9:24
1
find . -type f -name "abc" | xargs -I'{}' echo '{}' -p  | bash

or

find . -type f -name "abc" | sed 's/$/ -p/' | bash

Both commands first list the files that named "abc" before the first pipe. Between the first and second pipe, both command appends the parameter -p after each line. Finally, after the second pipe, these "abc" commands are sent to bash for execution.

5
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
    – Burgi
    Mar 8, 2017 at 11:24
  • @Burgi How is it not?
    – xuhdev
    Mar 8, 2017 at 18:17
  • You offer no explanation on what the commands do and it appears to be identical to the other answer here.
    – Burgi
    Mar 8, 2017 at 21:28
  • @Burgi Please point out which command is identical to the other answer.
    – xuhdev
    Mar 8, 2017 at 21:59
  • This is actually a good answer, worth my upvote. I too had the feeling that the OP knows enough to not need much explanation, but then I added it at his request.
    – simlev
    Mar 9, 2017 at 9:22
0

I would simply run this:

find . -type f -iname 'abc' -executable -execdir '{}' -p \;

This will run all abc executable files sequentially, with -p argument each time.

Details:

  • . Search in current folder and sub-folders
  • -type f Files only
  • -iname abc File name restriction (case insensitive)
  • -executable Restrict to executable files
  • -execdir ... \; Execute a command (exec) for each matching file (\;), with working directory set as the matching file folder (dir)
    • '{}' Placeholder replaced by matching executable file (ex: "./abc")
    • -p Argument given to executable file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .