Operating System - HP-UX
1833187 Members
2558 Online
110051 Solutions
New Discussion

Re: Can I use two "-exec" in a "find" command?

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

Can I use two "-exec" in a "find" command?


I want to find a filename, and ls -al on it, and also cat it. I want to use the following, but it could not work. What is wrong, any "connection" symbol I should use between these two -exec?

find / -name 'filename' -exec /bin/ls -al {} -exec /usr/bin/cat {} \;

Thanks,

none
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Can I use two "-exec" in a "find" command?

Hi Hanry:

You need to substitute a simple script to run the multiple commands you want:

# find / -name filename -exec /tmp/myscript {} \;

# cat /tmp/myscript
#!/usr/bin/sh
ls -l "$@"
cat "$@"

Regards!

...JRF...



TTr
Honored Contributor
Solution

Re: Can I use two "-exec" in a "find" command?

You can combine multiple directives in the find command with "-a" (logical AND) operator

find / -name 'filename' -exec /bin/ls -al {} \; -a -exec /usr/bin/cat {} \;
Hanry Zhou
Super Advisor

Re: Can I use two "-exec" in a "find" command?


TTr, That is exactly what I am looking for.

But, I am also interested in JFR's, but can I do that in the same file? It could not work though....

find / '.rhosts' -exec list {} \;
list ()
{
ls -l "$@"
cat "$@"
}
6 find / '.rhosts' -exec list {} \;
none
James R. Ferguson
Acclaimed Contributor

Re: Can I use two "-exec" in a "find" command?

Hi (again) Hanry:

If you would prefer not to have a small script that receives the 'find' output for post-processing, I think TTr has provided a straight-forward, simple solution!

I don't see a simple way to create one script as you secondarily asked.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Can I use two "-exec" in a "find" command?

Hi (again) Hanry:

On second thought, this works:

# cat ./myscript
#!/usr/bin/sh
function list {
for FILE in $@
do
ls -l "${FILE}"
cat "${FILE}"
done
}
list $( find /path -type f -print )
exit

Regards!

...JRF...
TTr
Honored Contributor

Re: Can I use two "-exec" in a "find" command?

You can do it in the same file as in the following

#!/usr/bin/sh
find / -name \.rhosts |
while read filename
do
ls -l $filename
cat $filename
done


If you leave out the first line (#!/usr/bin/sh) you can type the above script exactly as you see it on the command line and it will work too.