Operating System - HP-UX
1834650 Members
2069 Online
110069 Solutions
New Discussion

find but ignore one or two directories.

 
SOLVED
Go to solution
f. halili
Trusted Contributor

find but ignore one or two directories.

Hello,

Can find be use in a script to search for a file, ( let's say core ) and yet ignore one or two directories you do not want to search.

I tried gfind, but what happens is that it shows all files with the core sting on them.

Any thoughts?

Thanks,

f. halili
derekh
12 REPLIES 12
Sagar Sirdesai
Trusted Contributor

Re: find but ignore one or two directories.

Hi

For example you need to find core in /usr in FS but ignore /usr/local and /usr/etc we can use the below find command


find /usr -xdev -name "core" -path \(/usr/local -o /usr/etc\) -prune -o -print


Sagar
V. Nyga
Honored Contributor

Re: find but ignore one or two directories.

Hi,

you can exclude with 'grep -v', see also 'man grep'.

find . -type f -name core|grep -v

Well, of course it exludes every path with the same string , you have to know if this could be ...

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
f. halili
Trusted Contributor

Re: find but ignore one or two directories.

Hello Sagar,

I tried the syntax you gave but got the following below.

# /usr/bin/find /scratch -xdev -name "core" -path \(/scratch/TEMP -o /scratch/TEMP1\) -prune -o -print
find: bad option /scratch/TEMP1)


Thanks,
f. halili
derekh
f. halili
Trusted Contributor

Re: find but ignore one or two directories.

Hello Volkmar,

You suggeestion works but it will still go through the directories I want to ignore.

I want to skip some directories as they are huge, so don't want to go through them.


Thanks,
f. halili
derekh
V. Nyga
Honored Contributor

Re: find but ignore one or two directories.

Hi again,

then check the 'find' option '-prune':
-prune: If the current entry is a directory, cause find to skip that directory.

or better:

find . ! -path ./

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
V. Nyga
Honored Contributor

Re: find but ignore one or two directories.

Sorry,

-prune avoids from search in any sub-directory.

-path works without the '!':
find . -name core -path

V.
*** Say 'Thanks' with Kudos ***
V. Nyga
Honored Contributor

Re: find but ignore one or two directories.

Sorry, forget my last replies - it doesn't work like shown in man pages ...
*** Say 'Thanks' with Kudos ***
Sagar Sirdesai
Trusted Contributor

Re: find but ignore one or two directories.

Please check if this works

find /usr -xdev -name "core" -path /usr/local -o -print -path /usr/etc -prune -o -print

Sagar
V. Nyga
Honored Contributor

Re: find but ignore one or two directories.

Tested:

find . ! -path ./ -only|grep core

'!' semms to not work with option '-name'

V.

*** Say 'Thanks' with Kudos ***
sujit kumar singh
Honored Contributor
Solution

Re: find but ignore one or two directories.

Hi,


Please have a look at the belows, This might be helpful.

i have the fstab file at the following locations on my system.

# find / -name fstab
/etc/fstab
/usr/newconfig/etc/fstab
/var/sujit/etc/fstab
/var/sujit/etc/etc/fstab
# find / \( ! -path "/etc/*" \) -name fstab
/usr/newconfig/etc/fstab
/var/sujit/etc/fstab
/var/sujit/etc/etc/fstab
# find / \( ! -path "/etc/*" \) -a \( ! -path "/var/*" \) -name fstab
/usr/newconfig/etc/fstab
# find / \( ! -path "/etc/*" \) -a \( ! -path "/usr/*" \) -name fstab
/var/sujit/etc/fstab
/var/sujit/etc/etc/fstab
# find / -name fstab
/etc/fstab
/usr/newconfig/etc/fstab
/var/sujit/etc/fstab
/var/sujit/etc/etc/fstab


Hope this is helpful.


Regards
Sujit

f. halili
Trusted Contributor

Re: find but ignore one or two directories.

THANKS!
derekh
Dennis Handly
Acclaimed Contributor

Re: find but ignore one or two directories.