By glowblue


2019-12-02 21:43:29 8 Comments

I'm trying to write a regex to find files recursively with Mac Terminal (bash, not zsh even though Catalina wants me to switch over for whatever reason) using the find command. I'm looking for files that are:

  • Exactly 8 hexadecimal digits (0-9 and A-F)
  • But NOT only decimal digits (0-9)

In other words, it would match A1234567, ABC12DEF, 12345ABC, and ABCDABCD, but not 12345678 or 09876543.

To find files that are exactly 8 hex digits, I've used this:

find -E . -type f -regex '.*/[A-F0-9]{8}'

The .*/ is necessary to allow the full path name to precede the filename. This is eventually going to get fed to rm, so I have to keep the path.

It SEEMS like this should work to fulfill both of my requirements:

find -E . -type f -regex '.*/(?![0-9]{8})[A-F0-9]{8}'

But that returns an error:

find: -regex: .*/(?![0-9]{8})[A-F0-9]{8}: repetition-operator operand invalid

It seems like the find command doesn't support lookaheads. How can I do this without one?

3 comments

@oguz ismail 2019-12-02 21:51:23

With any POSIX-compliant find

find . -type f                 \
       -name '????????'        \
     ! -name '*[![:xdigit:]]*' \
       -name '*[![:digit:]]*' 

And if you insist on using regexps for this, here you go

find -E . -type f                     \
          -regex '.*/[[:xdigit:]]{8}' \
        ! -regex '.*/[[:digit:]]*'

Those who use GNU find should drop -E and insert -regextype posix-extended after paths to make this work.

@glowblue 2019-12-03 14:01:08

I don't insist on a particular method as long as it does what I need to do, though I appreciate having multiple ways to solve a problem. I didn't know about POSIX bracket expressions at all previously, so thanks for telling me about those. I'm no programmer, just trying to do an uncommon task more efficiently. I'm going to stick -exec rm {} \; at the end of this command (after a trial run with -i just to be sure). Thanks again!

@Paul Hodges 2019-12-02 22:10:35

My find didn't understand -E and was inexplicably grumpy about -regex in general, but this still worked:

find . -type f -name '[A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9]' -a -name '*[A-F]*'

Not as elegant as oguz ismail's, but easier to read for my clogged brain, lol

@user3439894 2019-12-02 23:11:04

The OP is tagged macos and also said "with Mac Terminal (bash...", so what version of macOS are you running that find doesn't accept the -E option?

@glowblue 2019-12-03 14:07:59

I think this is the solution I was originally heading towards, but I didn't realize find could accept more than one -name argument...

@Colin Fraizer 2019-12-02 22:02:43

It's probably easiest to just filter out the results you don't like:

find -E . -type f -regex '.*/[A-F0-9]{8}' -print | egrep -v '.*/[0-9]{8}$'

$ find -E . -type f -regex '.*/[A-F0-9]{8}' -print
./01234567
./ABCDEFAF
./ABCDEF01
./ABCDEF2A
./ABCDEFA2
./x/01234567
./x/ABCDEFAF
./x/ABCDEF01
./x/ABCDEF2A
./x/ABCDEFA2
$ find -E . -type f -regex '.*/[A-F0-9]{8}' -print | egrep -v '.*/[0-9]{8}$'
./01234567
./ABCDEFAF
./ABCDEF01
./ABCDEF2A
./ABCDEFA2
./x/01234567
./x/ABCDEFAF
./x/ABCDEF01
./x/ABCDEF2A
./x/ABCDEFA2

@user3439894 2019-12-02 23:07:59

In this particular case, I see no need to pipe the output of find to another command to do the filtering when find can do exactly what was expressed in the OP. See the answer posted by oguz ismail.

@Colin Fraizer 2019-12-05 22:07:56

A very good point! Thanks @user3439894.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Find all files with a filename beginning with a specified string?

  • 2010-10-27 15:26:44
  • RikSaunderson
  • 171923 View
  • 141 Score
  • 3 Answer
  • Tags:   bash find

3 Answered Questions

[SOLVED] bash find filenames with the same last four character

  • 2017-12-07 13:43:55
  • Eric
  • 60 View
  • 0 Score
  • 3 Answer
  • Tags:   bash find

5 Answered Questions

[SOLVED] How to create a hex dump of file containing only the hex characters without spaces in bash?

  • 2010-04-10 20:01:50
  • David Raswik
  • 210668 View
  • 174 Score
  • 5 Answer
  • Tags:   linux bash hex

2 Answered Questions

finding directories with regex

  • 2015-08-18 17:06:15
  • 3030tank
  • 93 View
  • 0 Score
  • 2 Answer
  • Tags:   regex bash

2 Answered Questions

5 Answered Questions

[SOLVED] OS X Find in bash with regex digits \d not producing expected results

2 Answered Questions

[SOLVED] BASH recursive find filename and copy to ftp

  • 2011-06-22 19:12:48
  • JRM
  • 3381 View
  • 2 Score
  • 2 Answer
  • Tags:   bash find

Sponsored Content