0

I need some help getting the find command to work right in a script working on. I need to be able to exclude some directories. Below I have what I'm doing as a work around.

sudo find / ( -name firefox -o -name thunderbird -o -name seamonkey \) -type f 2>/dev/null|grep -v '(10_Recommended|repo)'

I would like to exclude some directories so the script or find command doesn't take so long.

In the example above I want to exclude /export/repo, which is an NFS mount and /10_Recommended* which is the start of a patchset used on Solaris server. The current one being an example like: /10_Recommended_CPU_2014-01.

I have tried using -prune and ! statements and it's just not working. Can someone give me a hand with this?

I'm tuning this command on Solaris 10, RHEL 5, SLES 11 SP2.

4
  • Check out this SO question Mar 13, 2014 at 17:17
  • Thank you for posting links to other similar questions. I tried what was done in these posts and I still can't get the find command to exclude the directories.
    – Cyberninja
    Mar 13, 2014 at 17:34
  • It won't exclude everything you're looking to but I'm a big fan of the -xdev (also known as -mount) option. It won't cross filesystem boundaries so the NFS mount will not show up.
    – user143703
    Mar 13, 2014 at 18:05
  • @yoonix,Thanks for the option. The no mount option you gave me worked on the Solaris 10 server but not on the SuSe server. SLES gave me warring it wanted more arguments.
    – Cyberninja
    Mar 13, 2014 at 18:11

3 Answers 3

1

Try to add:

-not -path "/10_Recommended*" -not -path "/export/repo/*"

But be careful, The path of those repositories depends on where and how you execute the find command.

If you are at / and you do a find -name whatever, you must add a . in the not wanted path.

For you it would be:

find -name whatever -not -path "./10_Recommended*" -not -path "./export/repo/*"

But if you have add a / like you after your find command, you must remove those dots :

find / -name whatever -not -path "/10_Recommended*" -not -path "/export/repo/*"

1
  • Thank you very much. The answer you gave me works on the Linux server (in my case Red Hat & SuSe). It doesn't work on my Solaris servers though. Solaris doen't like the -not or -path options.
    – Cyberninja
    Mar 13, 2014 at 18:33
0

Try this:

find / \( -path /export -o -path "/*Recommended*" \) -prune -o \
  \( -name iceweasel -o -name thunderbird -type f \) -print 2>/dev/null

The reasoning here is that the -prune is a sort of postfix "action", best combined with -path. List the paths you want ignored first, then the files you're looking for. This seems unnatural in comparison with the prefix nature of other find operations.

In this manner, the paths are seen first to be pruned, and if not, the 'other side' of the -o switch executes. The other way around, it will depth search first and the prune never gets reached, or at least that's my understanding.

Portability is going to be a problem no matter what, especially between Solaris and *nix flavors. Maybe you'd have some luck with the version of find in OpenCSW but no guarantees there. Whip up a small shell script and check for the OS type from uname, a time-honored sysadmin tradition.

Here's an example from my machine, using GNU find:

[fuzzed@ops ~]$ find / -path /usr -prune -o \( -name rsync -o -name bash \) -print 2>/dev/null
[fuzzed@ops ~]$ find / -path /usr/share -prune -o \( -name rsync -o -name bash \) -print 2>/dev/null
/usr/bin/bash
/usr/bin/rsync

My find version:

[fuzzed@ops ~]$ find --version
find (GNU findutils) 4.5.11

Also, some predicates support globbing or regex, so check that out too.

1
  • Sam, thanks for replying to my post. I tried your answer on my Solaris 10 server and it didn't work. Solaris doesn't support the -path option. I alo couldn't get the version of the find command with the command you gave me.
    – Cyberninja
    Mar 13, 2014 at 19:48
0

Thanks for everyone's help. Below I have posted the answer to my question, or at least what worked for me. I test the below find command on Solaris 10, RHEL 5 and SLES 11.2 servers.

So below I have displayed the final find commands.

Searching for Java

# find / \( -name 10_Recommended* -o -name share \) -prune -o -type f -name java -print 2>/dev/null

Searching for Mozilla applications

# find / \( -name 10_Recommended* -o -name share \) -prune -o -type f \( -name firefox -0 -name thunderbird -0 -name seamonkey \) -print 2>/dev/null

I hope someone fins this useful.

Not the answer you're looking for? Browse other questions tagged .