44

I'm making a shell script to package some files. I'm zipping a directory like this:

zip -r /Users/me/development/something/out.zip /Users/me/development/something/folder/

The problem is that the resultant out.zip archive has the entire file path in it. That is, when unzipped, it will have the whole "/Users/me/development/anotherthing/" path in it. Is it possible to avoid these deep paths when putting a directory into an archive?

When I run zip from inside the target directory, I don't have this problem.

zip -r out.zip ./folder/

In this case, I don't get all the junk. However, the script in question will be called from wherever.

FWIW, I'm using bash on Mac OS X 10.6.

78

Your script should use cd or pushd and popd to move into the directory that will be the root of the archive before issuing the zip command. How you do this exactly will depend on how the script knows what to zip up. But, if you want /Users/me/development/something/folder zipped with internal paths of just ./folder, you'd need to do this:

pushd /Users/me/development/something
zip -r /path/to/out.zip ./folder/
popd

That will result in your out.zip containing the relative paths you want.

If you need assistance with scripting that, you'll need to show us your script.

  • 8
    no problem. i do this in one-liners on the commandline all the time, eg: $ pushd /some/path ; do-something ; popd ... or even with subshells: $ ( cd /some/path ; do-something ) – quack quixote Mar 14 '10 at 2:25
  • 1
    @~quack: +1 especially for the sub-shell technique in the comment. – Jonathan Leffler Mar 14 '10 at 4:39
  • 1
    Though using && instead of ; is a good idea so that the other command does not run if the cd failed (typo, or other problem): (cd /some/path && do-something) – Chris Johnsen Mar 14 '10 at 6:16
  • 3
    Is there not a way to do this without specifying the absolute path for the zip file? – orange80 Jun 28 '13 at 7:18
  • 2
    Why are you playing games with the path? Why not use zip -j? – jww Jan 4 '15 at 1:15
3

The problem is that the resultant out.zip archive has the entire file path in it.
...
Is it possible to avoid these deep paths when putting a directory into an archive?

Yes. Use the -j option with zip. -j is "junk the path". According to the man page on zip:

Store just the name of a saved file (junk the path), and do not store directory names. By default, zip will store the full path (relative to the current directory).

Using -j means the following command:

zip -j myarchive.zip file1.txt dir1/file2.txt dir2/dir3/file3.txt ../file4.txt

Will create an archive that looks like:

myarchive.zip
    |
    +-- file1.txt
    +-- file2.txt
    +-- file3.txt
    +-- file4.txt
  • 4
    Doesn't this also junk all paths, even those in the 'folder' directory? I think the OP only wants to get rid of the path on the command line. – Peter Jaric Sep 14 '15 at 7:37
  • 1
    This might not be the answer for this question, but it's what I need. – Aaron McMillin Mar 16 '17 at 0:50
  • This answer incorrectly assumes user wants a flat zip. This is a bad assumption and can cause the script to fail if two files in different sub-folders have the same name. – Andrew Schwartz Feb 12 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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