2

I want to find all files in a directory that match a given pattern say A and don't match a given pattern say B

What I've tried: Doing "ls -I B" gives me all files which match the pattern B. But I'm not getting any leads on how to do what I want to.

dotgc
  • 500
  • 1
  • 7
  • 21

1 Answers1

8

You can use find . -name "pattern" and then add a negative condition for the pattern you don't want to have:

find . -name "pattern_A" ! -name "pattern_B"

Example

Let's look for those files whose name contains a A but not a B:

$ ls -1
adfadAadsa
adfBasdA
Aksjdflksj
asdfBasdf
Badsf

$ find . -name "*A*" ! -name "*B*"
./Aksjdflksj
./adfadAadsa
fedorqui
  • 275,237
  • 103
  • 548
  • 598