0

I'm trying to find all files whose name matches certain C++ file extensions but exclude certain directories matching a pattern with this:

find /home/palchan/code -name "*.[CcHh]" -o -name "*.cpp" -o -name "*.hpp" -a ! -name "*pattern*"

and this still gives me as output certain files like:

/home/palchan/code/libFox/pattern/hdr/fox/RedFox.H

which has the pattern in it?

Here is an example:

> ls -R .
.:
libFox

./libFox:
RedFox.C  RedFox.H  pattern

./libFox/pattern:
RedFox.C  RedFox.H

and then I run:

> find . \( -name "*.[HC]" -a ! -name "*pattern*" \)
./libFox/pattern/RedFox.C
./libFox/pattern/RedFox.H
./libFox/RedFox.C
./libFox/RedFox.H

2 Answers 2

1

The following should work:

find /home/palchan/code \( -name "*pattern*" \) -prune -o -type f \( -name "*.[CcHh]" -o -name "*.cpp" -o -name "*.hpp" \) -print

From man find:

 -name pattern
          Base of file name (the path with the leading directories removed) matches shell pattern pattern.  The metacharacters (`*', `?', and `[]')  match
          a  `.'  at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CONFORMANCE below).  To ignore a directory and
          the files under it, use -prune; see an example in the description of -path.  Braces are not recognised as being special, despite the  fact  that
          some  shells  including  Bash  imbue  braces  with  a special meaning in shell patterns.  The filename matching is performed with the use of the
          fnmatch(3) library function.   Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell.

So, basically, you should use -prune to exclude directories instead of ! -name something

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I'm curious, why does one need the ( stuff?
The \( and \) are just parenthesis used for grouping, and escaped from the shell, they work like regular parenthesis in boolean conditions
0

Try doing this :

find /home/palchan/code \( -name "*.[CcHh]" -o -name "*.cpp" -o -name "*.hpp" -a ! -name "*pattern*" \)

1 Comment

This isn't working? I edited my answer with an example and the sample output this produces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.