How can I automatically ZIP files with same file names (different extensions) into one ZIP?

For example I have four files

  • king.jpg
  • king.txt
  • queen.jpg
  • queen.txt

which would led to

  • king.zip (containing king.jpg and king.txt)
  • queen.zip (containing queen.jpg and queen.txt)

You can easily do this with a short bash snippet.

for i in *; do
    if [[ "$p" != "$f" ]]; then
        zip "$f" "$f".*

If you are not familiar with Terminal/bash and don't plan to use this too often, you can do the following to use it:

  • create a folder where you move all your kings and queens to (and nothing else)
  • open Terminal, type cd followed by a space, drag the folder you've just created into the Terminal window (release the mouse button once a green cross appears) and press Enter
  • type p=SOME.CRAZY.STUFF followed by Enter
  • type (or even better copy/paste)

    for i in *; do f="${i%.*}"; if [[ "$p" != "$f" ]]; then zip "$f" "$f".*; fi; p="$f"; done

    again followed by Enter

  • (assuming no error message etc was displayed) quit Terminal again
| improve this answer | |
  • thanks patrix. this one looks interesting. would you like to expand how to use it. – sukema Dec 16 '15 at 13:17
  • I assume you are not familiar with Terminal/bash then? I'll update the answer later (=> when I'm in front of a Mac again). – nohillside Dec 16 '15 at 13:25

you can put both into one folder and zip that folder..

otherwise you need to use tar, which is a bit more complicated...

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .