2

I have a directory with ~8000 files of the form

output/Manuscript_00750_AnimalGiants-compact.json
output/Manuscript_00750_AnimalGiants-expanded.json
output/Manuscript_00750_AnimalGiants.json
output/Manuscript_00752_AnimalGiants-compact.json
output/Manuscript_00752_AnimalGiants-expanded.json
output/Manuscript_00752_AnimalGiants.json
output/Unit_TZH_12345_Foo-compact.json
output/Unit_TZH_12345_Foo-expanded.json
output/Unit_TZH_12345_Foo.json

I need to come up with a regex to work with the find tool to select just the Manuscript-compact ones:

output/Manuscript_00750_AnimalGiants-compact.json
output/Manuscript_00752_AnimalGiants-compact.json

Coming up with the regex is the easy part, but getting find to cooperate is the hard part.

Here's my regex:

/Manuscript[0-9_a-zA-Z]+-compact\.json/

Here are some of the commands I've tried; all produce zero results. The cwd is the directory above output/:

find output -regex "Manuscript[0-9_a-zA-Z]+-compact\.json"
find output -regex "\./output/Manuscript[0-9_a-zA-Z]+-compact\.json/"
find output -regex ".*\Manuscript[0-9_a-zA-Z]+-compact.*\json"

But this command does produce results - it selects all the files that start with "Manuscript", which is obviously too broad:

find output -regex ".*\Manuscript.*\json"

What's the correct regex format for find here?

commanda
  • 4,841
  • 1
  • 25
  • 34

1 Answers1

5

On OSX you can use this find with extended regex:

find -E output -regex '.*/Manuscript[0-9_a-zA-Z]+-compact\.json$'

On gnu find use:

find output -regextype posix-extended -regex '.*/Manuscript[0-9_a-zA-Z]+-compact\.json$'
commanda
  • 4,841
  • 1
  • 25
  • 34
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • The first one works - thanks! Can you explain why though? What's with the `'.*/` at the beginning? – commanda Sep 27 '16 at 14:47
  • 1
    `.*/Manuscript `matches 0 or more characters before slash and `Manuscript`. Even when this file is in current directory there will be `./` before `Manuscript` – anubhava Sep 27 '16 at 14:50
  • 1
    For reference, the `-E` option is derived from FreeBSD, and should work in NetBSD, DragonflyBSD, but perhaps not OpenBSD. – ghoti Sep 27 '16 at 15:10
  • @commanda: You may consider accepting this answer :) – anubhava Sep 28 '16 at 06:38