1

Most people are familiar with using find to run the same command on a number of files, e.g.:

find -type f | xargs command

or

find -type f -exec command {} \;

My question is whether it's possible to run multiple commands from find:

find [path] [expression] -exec command1 {} -exec command2 {} \;

so that the output would look like

command1: file1
command2: file1
command1: file2
command2: file2
command1: file3
command2: file3
command1: file4
command2: file4

Obviously I can write a simple shell script wrapper for this, but is it possible to do it all from the command line (perhaps using awk, tee etc.)?

2 Answers 2

2

Or

find . -exec echo {} \; -exec echo {} \;
1

You could do something simple like this.

find . -exec bash -c 'echo {} ; echo {} ' \;
3
  • Along with some "{}", this is perfect!
    – Andrew
    Oct 8, 2010 at 6:16
  • ... ok, I'll have to give it to the other one after all; it does actually give multiple -exec options. But this is also good.
    – Andrew
    Oct 11, 2010 at 2:49
  • 1
    This will break for commands other than echo if the filename contains spaces. This will even break for echo if a filename contains multiple consecutive spaces. The arguments passed to the command with {} are unquoted, and are thus separate arguments to the command. There is no way to properly quote the {}. To fix this, use the following syntax: find . -exec bash -c 'echo "$1"; echo "$1" ' _ {} \; where the _ will be $0 in bash, and {} will be $1, properly quoted. Or use JRT's solution above, which will properly pass arguments with spaces.
    – nash
    Sep 12, 2017 at 18:35

You must log in to answer this question.

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