Copy a range of files in command line (ZSH/BASH)

Solution 1:

You were very close. Your question was almost the correct syntax:

cp P10802{75..83}.JPG ~/Images

Solution 2:

To iterate over a range in bash:

for x in {0..10}; do echo $x; done

Applying the same in your case:

for x in {272..283}; do cp P1080$x.JPG ~/Images; done

Solution 3:

Zsh, with the extendedglob option has the globbing (pattern matching) operator.

setopt extendedglob
echo P10802<75-83>.JPG

will match filenames in the current directory that match that pattern (beware that P1080275.JPG matches but so does P108020000000075.JPG)

On the other end, the {x...y} string expansion operator (supported by zsh and recent versions of bash and ksh93), expands to the strings from x to y, regardless of what files there are in the current directory.

cp P10802<75-83>.JPG ~there

will copy the matching files, so will

cp P10802{75..83}.JPG ~there

But you'll get errors if for instance P1080281.JPG doesn't exist.

Tags:

Linux

Bash

Copy

Zsh