Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with find and -exec command

Status
Not open for further replies.

jvbpro

Programmer
May 27, 2002
9
CA
Hi all
Does anyone know how to use multiple -exec command in a find. I need to execute more than one command for each file found in the directory.

eg. find . -name "*.txt" -exec something {}; -exec somethingelse {}; -exec somethingelse {};
Thank you in advance
Any help is appreciated.
Jeya.
 
See man pages for find - it states that rm is the only command allowed in the -exec statement - so you won't want (or be able) to delete the same files 3 times anyway ???
HTH ;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Which is wrong - so ignore my previous reply !
(Still dunno how you'd do it, either)
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
But now I've got the answer - leastways for Aix 4.3.3
find . -name "*.txt" -exec something {} \; -exec somthingelse \;
the find command is all on 1 line
;-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
Thanks for your help.

I have another question... DO you know how to pass each file found as a parameter into a variable so that I could use it into the -exec command?

thank you again.
 
An example:
Code:
find . -type f | xargs -n1 -i sh -c 'echo {}; touch {}'
Cheers, Neil
 
I haven't tested this but try :
infile=$1 #If a passed parameter
find . -name "${infile}" -exec .....etc etc
or .....
if looking for several files :
for infiles in yourfilenames # eg logfile* for a few files named logfilea logfileb etc etc
do
find . -name "${infiles}" -exec .....etc etc
done

or ........
listoffiles="logfilea logfileb logfilec"
for infiles in $listoffiles
do
find . -name "${infiles}" -exec .....etc etc
done
;-)


Dickie Bird
db@dickiebird.freeserve.co.uk
 
A more useful example:
Code:
find . -type f -name "*.tar" | xargs -n1 -i sh -c 'compress -f {}; uuencode {}.Z {}.Z > {}.uue'
Cheers, Neil :)
 
thank you all... but could be more descriptive with that find command that you just wrote?

This is what I did though to get the things working...

find . -name "*.exp" -exec newfilename.sh {} \;

newfilename.sh will contain all the commands that I want to execute with each file found... I used $1 to pass in the name of the file as parameter into it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top