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

Accepted answer
Score: 61

I assume you want to copy these files to 1 another directory:

cp -t target_directory foo_{0..54}.jpg
Score: 10

I like glenn jackman answer, but if you really want to 9 use globbing, following might also work 8 for you:

$ shopt -s extglob
$ cp foo_+([0-9]).jpg $targetDir

In extended globbing +() matches one 7 or more instances of whatever expression 6 is in the parentheses.

Now, this will copy 5 ALL files that are named foo_ followed by any 4 number, followed by .jpg. This will include 3 foo_55.jpg, foo_139.jpg, and foo_1223218213123981237987.jpg.

On second thought, glenn jackman 2 has the better answer. But, it did give 1 me a chance to talk about extended globbing.

Score: 4

ls foo_[0-9].jpg foo_[1-4][0-9].jpg foo_5[0-4].jpg

Try 2 it with ls and if that looks good to you 1 then do the copy.

Score: 4
for i in `seq 0 54`; do cp foo_$i.jpg <target>; done


Score: 0

An extension answer of @grok12's answer 3 above

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

Basically the regex above will match 2 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 1 result with regex below

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

More Related questions