50
13
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.
2Why are you playing games with the path? Why not use
zip -j
? – jww – 2015-01-04T01:15:38.683Perfect. Thanks for the shell command schooling. I like the idea of pushing and popping paths onto the shell "stack". – jerwood – 2010-03-14T02:16:08.847
8no 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 – 2010-03-14T02:25:27.6301@~quack: +1 especially for the sub-shell technique in the comment. – Jonathan Leffler – 2010-03-14T04:39:21.200
1Though using
&&
instead of;
is a good idea so that the other command does not run if thecd
failed (typo, or other problem):(cd /some/path && do-something)
– Chris Johnsen – 2010-03-14T06:16:23.087@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 – 2010-03-14T10:11:17.593Life-saving answer. I was looking for option to only zip relative paths while running the script from root. Thanks a lot! – Moseleyi – 2016-02-27T20:35:43.727
@jww i can verify that using the junk-paths argument didn't work for my senario. i needed to cd to the directory. – Andy – 2016-11-23T15:46:19.560
assuming,
/path/to
was your previous working directory, you could usezip -r ~1/out.zip ...
(directory stack shortcuts) – phil294 – 2017-06-13T15:47:50.777zip -j works for me and thus, it zips the files inside that folder without including the folder name. – Jun – 2018-09-14T00:11:41.500
3Is there not a way to do this without specifying the absolute path for the zip file? – orange80 – 2013-06-28T07:18:09.800
@orange80 - I was reading over this Q&A and wondered the same thing. I did a little research and found what seems to be a solution, but I am definitely a bash novice, so take it with a grain of salt. If you want the zip file to be in the directory you are navigating from, you can use dirs and awk to get that directory. I assign that to a variable and use it in my script:
LASTDIR=\
dirs | awk '{print $2}'``, then instead ofzip -r /path/to/out.zip ./folder/
, I dozip -r $LASTDIR/bundle.zip
– ajh158 – 2014-01-10T16:01:29.883