Can I use shell wildcards to select filenames ranging across double-digit numbers (e.g., from foo_1.jpg to foo_54.jpg)?

Viewed 19648

I have a directory with image files foo_0.jpg to foo_99.jpg. I would like to copy files foo_0.jpg through foo_54.jpg.

Is this possible just using bash wildcards?

I am thinking something like cp foo_[0-54].jpg but I know this selects 0-5 and 4 (right?)

Also, if it is not possible (or efficient) with just wildcards what would be a better way to do this?

Thank you.

5 Answers

An extension answer of @grok12's answer above

$ ls foo_([0-9]|[0-4][0-9]|5[0-4]).jpg

Basically the regex above will match below

  • anything with a single digit OR
  • two digits and that first digit must be between 0-4 and second digit between 0-9 OR
  • two digits and that first digit is 5 and second digit between 0-9

Alternatively you can achieve similar result with regex below

$ ls file{[0-9],[0-4][0-9],5[0-4]}.txt
Related