Problem
I need to make a zip file with the following command:
zip /dir/to/file/newZip /data/to/zip/data.txt
This works, however the resulting zip file creates a directory structure that is identical to the raw file’s directory structure. There are a lot of folders I don’t require.
A quick scan of the man page and a Google search yielded no results.
Asked by Jake
Solution #1
You can use the -j option.
-j
--junk-paths
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).
Answered by Lars Kotthoff
Solution #2
The -j option will not function with the -r option. As a result, here’s a workaround:
cd path/to/parent/dir/;
zip -r complete/path/to/name.zip ./* ;
cd -;
Or in-line version
cd path/to/parent/dir/ && zip -r complete/path/to/name.zip ./* && cd -
If you don’t want the cd – output to appear on screen, you can route it to /dev/null.
Answered by Vikas Tawniya
Solution #3
Use the -j option:
-j 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 path).
Answered by Dan D.
Solution #4
In a similar vein, I was looking for a way to do the same thing using directories. Unfortunately, the -j option isn’t applicable in this case:(
Here’s an excellent way to go about getting things done: https://superuser.com/questions/119649/avoid-unwanted-path-in-zip-file
Answered by flaky
Solution #5
Alternatively, you could create a temporary symbolic link to your file:
ln -s /data/to/zip/data.txt data.txt
zip /dir/to/file/newZip !$
rm !$
This also works for a directory.
Answered by MirkoBanchi
Post is based on https://stackoverflow.com/questions/9710141/create-zip-file-and-ignore-directory-structure