3

I need to find and archive files with a certain file name e.g. ABC_000.jpg

find ~/my-documents/ -iname "ABC_***.JPG" -type f -exec cp {} ~/my-documents/archive/ \;

however I can not seem to find a way to limit the find function to find only 3 integers as there are files that are named ABC_CBA.jpg that I do not want included

WhereAreYouSyntax
  • 163
  • 2
  • 4
  • 13

1 Answers1

6

Try this find:

find ~/my-documents/ -iname "ABC_[0-9][0-9][0-9].JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;

EDIT: Or using regex:

find -E ~/my-documents/ -iregex ".*ABC_[0-9]{3}\.JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Good one; you can also use the option `-regex` to match the specific name (useful if more complicated): `find ~/my-documents/ -iregex "ABC_[0-9]{3}.JPG" -type f -exec cp '{}' ~/my-documents` – Bentoy13 Nov 20 '13 at 15:04
  • @Bentoy13: Thanks, I was actually going to post same with my original answer but it didn't work for me on OSX and i got busy in my work. Now when I got some time to look at `man find` again it occurred to me that `find -E` would come to party in that case. Answer edited. – anubhava Nov 20 '13 at 15:22