Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a source tree which, when make is run, produces several executables named "001", "002", and etc. I'm trying to write a script which will find all of these executables in my source tree, and then execute them. I have this so far:

find build/ -type f -executable | ack --nocolor "\d{3}$"

Which lists the executables that I want to execute correctly.

My question is, how do I then run all of them? I thought perhaps some combination of xargs and exec would do it, but exec seems to try replacing the current shell with the command, which isn't what I want.

share|improve this question

4 Answers 4

up vote 2 down vote accepted

Try:

$ find build/ -type f -executable | ack --nocolor "\d{3}$" |
while read prog
do
    "$prog"
done
share|improve this answer
    
I think @1_CR's answer is cleaner to just execute the files, but I'm accepting this because it turns out that it's useful to be able to do other stuff inside the while besides just executing the file, and it's trivial to extend this version. Thanks! :) –  CmdrMoozy Aug 14 at 18:01
1  
@CmdrMoozy, you don't need to step outside find in order to loop. For example find build/ -type f -name '*[0-9][0-9][0-9]' -executable -exec bash -c 'for f; do cksum "$f"; done' _ {} + –  1_CR Aug 14 at 18:12

This may work

find build/ -type f  -executable -exec sh -c 'exec "$1"' _ {} \;

Or to filter to just the 001, 002 .. files

find build/ -type f -name '*[0-9][0-9][0-9]' -executable  -exec sh -c 'exec "$1"' _ {} \;
share|improve this answer
    
The OP want to filter the result of find before execute it. –  cuonglm Aug 14 at 17:47
    
@Gnouc, I am not sure if he's using ack for listing or for filtering. Anyways, i'll add a filter –  1_CR Aug 14 at 17:51
    
ack is basically a nicer version of grep - so it's mostly for filtering. This build system produces some other files that are marked executable besides the actual compilation results. –  CmdrMoozy Aug 14 at 17:52
    
@CmdrMoozy and Gnouc, added filtering –  1_CR Aug 14 at 17:55
    
+1 because you solved the problem using only find. Thanks! :) –  CmdrMoozy Aug 14 at 18:00

One of the things find is good at is selecting files that match criteria, so you can use it to pick out the files whose names consist of three digits.  And then you can use this simpler form:

find build -name "[0-9][0-9][0-9]" -type f -executable -exec {} ";"

And you might be able to use -regex '.*/\d{3} if you can find the right -regextype to go with it.

share|improve this answer
find -type f -name '00[123]' -exec env - {} \;

Add in whatever environment you want to declare for the different executables. Maybe 2>/dev/null if there are non-executable 00[123] files lying around and you don't want to hear about it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.