Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory that has 800 files like this:




... etc

I need to create zip files like this

file1.zip (containing file1.png and file1@2x.png)
file2.zip (containing file2.png and file2@2x.png)
file3.zip (containing file3.png and file3@2x.png)

Is there a magic command I can use to do that?

share|improve this question
up vote 5 down vote accepted

If always there are two, is simple:

for f in file+([0-9]).png; do zip "${f%png}zip" "$f" "${f/./@2x.}"; done

Note that the above will work as is from the command line. If you intend to use it in a script, put shopt -s extglob somewhere before that line. (extglob is enabled by default only in interactive shells.)

In old bash not handling extended patterns this ugly workaround will work, but better change the logic as suggested in another answer by Leonid:

for f in file[0-9].png file*[0-9][0-9].png; do zip "${f%png}zip" "$f" "${f/./@2x.}"; done
share|improve this answer
I am receiving this error after trying your command... -bash: syntax error near unexpected token `(' – SpaceDog Nov 20 '12 at 15:43
I am trying it from command line and seeing this error – SpaceDog Nov 20 '12 at 15:47
Which bash version you use? Mine is 4.2.37. – manatwork Nov 20 '12 at 15:48
extglob is not enabled by default in interactive shells, but maybe your OS ships with a bashrc that turns it on (and extglob has been added to bash in 2.02-alpha1) – Stéphane Chazelas Nov 20 '12 at 21:25
No, --enable-extended is enabled by default (opt_extended_glob=yes in configre), but not --enable-extended-glob-default (opt_extglob_default=no), that's an inaccuracy in the documentation. It must have been explicitly enabled by your OS maintainer. But point taken about different bash distributions having different defauts. – Stéphane Chazelas Nov 21 '12 at 8:19

How about a slightly modified version of what was suggested by manatwork? Something like this: for f in file*@2x.png; do zip ${f%@2x.png}.zip $f ${f/@2x./.}; done

It iterates over the 'second in pair' files only, and there's no regexp in the file list generation.

share|improve this answer
THANKS!!!!!!!!!!! – SpaceDog Nov 20 '12 at 16:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.