David542
David542

Reputation: 110572

Globbing pattern: exclude a directory

I have the following filepaths

/vol/form/various.txt
/vol/var/sender.py
/vol/var/hello.txt

I would like to get all .txt files that do not have the directory of form in them. For example, something like:

*.txt AND ! */form/*

What would be the correct globbing pattern to do this -- i.e, in a single pattern (or is that not possible)?

Upvotes: 2

Views: 1504

Answers (2)

dawg
dawg

Reputation: 104102

You can negate a grep:

find /vol -type f -name '*.txt' | grep -v '/form/'

If you want a single regex you need to either find file names with find and feed to GNU grep:

find /vol -type f | ggrep -oP "(?!.*?\/form\/)(^.*\.txt$)" 

or use Perl with that same regex:

perl -MFile::Find -e 'find sub {
      print "$File::Find::name\n" if -f && m/(?!.*?\/form\/)(^.*\.txt$)/
      },"/vol/"'

Explanation of the regex:

(?!^.*?\/form\/)(^.*\.txt$)

^              ^                 Negative lookahead fail on /form/
                 ^       ^       Anchors for start and end of string
                   ^             all horizontal characters
                     ^^          literal . 
                                 (if not escaped it would match any character)
                       ^         txt extension 

Demo

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185841

Like this:

find /vol ! -path '*/form/*' -type f -name '*.txt'

If you need to negate a pattern, like with a regex, AFAIK look around are not supported by find -regex*. So it's not possible only with one find regex expression.

Upvotes: 1

Related Questions