Operating System - HP-UX
1752794 Members
6413 Online
108789 Solutions
New Discussion юеВ

Exclude some dirs from find.

 
Ranoop Nambiar
Advisor

Exclude some dirs from find.

I have a little complex requirement, need to find the presents of *.sh and *ksh scripts, which are using remsh |rcp inside!! .. We would need to replace the remsh with SSH. Thus, in the first stage, I need to find all the scripts which uses remsh in all 50 (hp-ux servers). Now, we have jump boxes from there, we can remsh to all these 50 boxes. I was trying to run a find command in for loop..

DIRS=`/sysadmin/ssh_admin/bdfmegs|awk '{print $6}'|grep -v Mounted`
SERVERLIST=/sysadmin/ssh_admin/ssh_script/serverlist_test.txt
OUTPUT=/sysadmin/ssh_admin/output/$(hostname).txt
for i in `cat $SERVERLIST`
do
remsh $i /usr/bin/find $DIRS -xdev \( -name "*.sh" -o -name "*.ksh" \) -exec egrep -q 'remsh|rcp' '{}' \; -print >>$OUTPUT
done
exit

I m struck in some areas, where I need to exclude the .snapshots ( netapp volms) directories from the findтАж. , Awaiting tips from u allтАж.

Thanks,
Ranoop
5 REPLIES 5
Bob E Campbell
Honored Contributor

Re: Exclude some dirs from find.

What is unique about the netapp (and other) directories that will allow you to ignore them? You can get as complex as you like by dropping the exec and just feeding the initial list to a shell script.
James R. Ferguson
Acclaimed Contributor

Re: Exclude some dirs from find.

Hi Ranoop:

Something like this would enable you to skip the visit of directories:

# find /somepath -type f -a ! -path "/somepath/dir1/*" -a ! -path "/somepath/dir2/*" -name "*.sh" -exec ls -l {} +

Note that your execution will be much more efficient if you use the "+" terminator instead of the ";" terminator for your '-exec'.

Regards!

...JRF...


Alex Glennie
Honored Contributor

Re: Exclude some dirs from find.

And would the netapps volumes be mounted or more than one server in some instances ? Just a thought.
Ranoop Nambiar
Advisor

Re: Exclude some dirs from find.

Hi,

Something like this has done the job, -:)

/usr/bin/find $DIRS -xdev \( -name "*.*sh" -a ! -path "*/.snapshot/*" \) -exec egrep -q 'remsh|rcp' '{}' \; -print >>$OUTPUT

But the time taken for the execution is too long, it brings box perfomance down as well..
Any modifications on this would be highly appreciated..

Thanks
Dennis Handly
Acclaimed Contributor

Re: Exclude some dirs from find (-prune)

>where I need to exclude the .snapshots (netapp volumes)

Ding ding! Been there, done that.
The only reasonable solution is using find -prune. (Unless there is some easier gnu find option?)

for i in $(< $SERVERLIST); do
    remsh $i -n "find $DIRS -xdev -name .snapshot -prune -o \( -name '*.sh' -o -name '*.ksh' \) \
    -exec grep -l -e remsh -e rcp {} + " >> $OUTPUT
done

This removes the egrep hammer and uses multiple grep -e. This removes the -print and uses grep -l. This is needed if using JRF's "-exec ... +".

>Bob: What is unique about the netapp directories that will allow you to ignore them?

These are backups. You don't want to search them. For each directory level, you then have N more subdirectories and it takes "days" to search. That's why -prune.

>JRF: Something like this would enable you to skip the visit of directories:

No, this visits them but doesn't print them. Useless if you want to finish in a timely manner.

>Note that your execution will be much more efficient

Without -prune, this doesn't matter but it is a good practice.

>Alex: And would the netapps volumes be mounted on more than one server

Possibly, so you wouldn't want to search it for each of the SERVERLIST.