2

I'm doing a file-find operation and I want to exclude all GIT hash files, which have names that are 38 hexadecimal numbers.

But I don't think it's too elegant to write

find . -not -name "[0-9a-f]" <-- 38 times.

Is there another way?

Sun
  • 43
  • 4

1 Answers1

1

Use brackets:

find . -not -regex ".*[0-9a-f]\{38\}"

Escaped "curly brackets" -- { } -- indicate the number of occurrences of a >preceding RE to match.

It is necessary to escape the curly brackets since they have only their literal >character meaning otherwise. This usage is technically not part of the basic RE >set.

"[0-9]{5}" matches exactly five digits (characters in the range of 0 to 9).

http://tldp.org/LDP/abs/html/x17129.html

Note: earlier answer (find . -not -name "[0-9a-f]\{38\}") was incorrect; as pointed out by @Cyrus, the -regex option is required. In addition, you must use .* as a prefix (or something equivalent) to allow find to match the directory (perhaps this is clear, but it tripped me up when I was testing solutions).

Small example:

ls -C1
ab
abab
abcbade
ac
acba
acca
adda
d

find . -regex ".*[a-c]\{4\}"
./abab
./acba
./acca

find . -not -regex ".*[a-c]\{4\}"
.
./ab
./abcbade
./ac
./adda
./d
kadnarim
  • 177
  • 6
  • 2
    2 upvotes for a wrong answer? Option `-name` uses globbing syntax. GNU find‘s option `-regex` uses regex. – Cyrus Jul 11 '17 at 03:58