3

I have a directory that has 800 files like this:

file1.png
[email protected]

file2.png
[email protected]

file3.png
[email protected]

... etc

I need to create zip files like this

file1.zip (containing file1.png and [email protected])
file2.zip (containing file2.png and [email protected])
file3.zip (containing file3.png and [email protected])

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

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Duck
  • 4,434
  • 19
  • 51
  • 64

2 Answers2

5

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
manatwork
  • 30,549
  • 7
  • 101
  • 91
  • I am receiving this error after trying your command... -bash: syntax error near unexpected token `(' – Duck Nov 20 '12 at 15:43
  • I am trying it from command line and seeing this error – Duck Nov 20 '12 at 15:47
  • Which `bash` version you use? Mine is 4.2.37. – manatwork Nov 20 '12 at 15:48
  • GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)... this is mountain lion OSX – Duck Nov 20 '12 at 15:49
  • I added this line of yours to a script, and it works partially. Only the regular file is included in the zip, not the @2x... – Duck Nov 20 '12 at 16:01
  • YESSSSSSSSSSSSSSSSSSSSSSSSS!!!!!!!!!!!!!!!!!!! THANKS!!!!!!!!! – Duck Nov 20 '12 at 16:10
  • 1
    `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
  • @StephaneChazelas, none of my rc files contain `extglob` setting. `extglob` defaults to what `bash` `configure`'s `--enable-extended-glob-default` switch indicated. And that switch is “enabled by default, unless the operating system does not provide the necessary support”. This may be the difference between Linux and OSX behavior. – manatwork Nov 21 '12 at 07:39
  • 1
    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 08:19
  • Thank you @StephaneChazelas, I will investigate on this. – manatwork Nov 21 '12 at 08:34
4

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.

Leonid
  • 953
  • 6
  • 9