I want to run two commands but the second command depends on the first.
Is there a way to do something like this.
find . -name '*.txt' -exec 'y=$(echo 1); echo $y' {} \;
…
And actually, I want to do this.
Run the find command, change to that directory that the file is in and then run the command on the file in the current directory.
find . -name '*.txt' -exec 'cd basedir && /mycmd/' {} \;
How do I do that?
Find’s
-execoption expects an executable with arguments, not a command, but you can usebash -c cmdto run an arbitrary shell command like this:I have added
pwdto confirm thatmycmdexecutes in the right directory. You can remove it.dirnamegives you the directory of each file andbasenamegives you the filename. If you omitbasenameyour command will receive (as{}) pathname to each file relative to the directory where you runfindwhich is different frommycmd‘s current directory due tocd, somycmdwill likely fail to find the file. If you want your command to receive absolute pathname, you can try this: