Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 25 down vote accepted

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.

share|improve this answer
    
Perfect. Thanks for the shell command schooling. I like the idea of pushing and popping paths onto the shell "stack". –  jerwood Mar 14 '10 at 2:16
4  
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
    
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
    
@Chris Johnsen: that's a good tip. i don't use && and || as often in my one-liners as i do in scripting, but that's part personal style and part personal failing. in this case it is the more suitable grammar. –  quack quixote Mar 14 '10 at 10:11

Incidentally, in GNU tar, there's a -C command line argument to change directory before doing anything. That can save some pushd/popd action when you're using tar.

share|improve this answer

Your Answer

 
discard

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.