Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using find command on in a Bash Script to find integers

Tags:

linux

find

bash

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

like image 503
WhereAreYouSyntax Avatar asked Mar 04 '26 23:03

WhereAreYouSyntax


1 Answers

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/ \;
like image 199
anubhava Avatar answered Mar 07 '26 19:03

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!