Create zip file and ignore directory structure

LinuxZip

Linux Problem Overview


I need to create a zip file using this command:

zip /dir/to/file/newZip /data/to/zip/data.txt

This works, but the created zip file creates a directory structure mimicking the directory to the raw file. It is a lot of extra folders that I don't need.

I didn't find an answer in a cursory glance over the man page or a Google hunt.

Linux Solutions


Solution 1 - Linux

You can use -j.

-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).

Solution 2 - Linux

Using -j won't work along with the -r option.
So the work-around for it can be this:

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 -

you can direct the output to /dev/null if you don't want the cd - output to appear on screen

Solution 3 - Linux

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).

Solution 4 - Linux

Somewhat related - I was looking for a solution to do the same for directories. Unfortunately the -j option does not work for this :(

Here is a good solution on how to get it done: https://superuser.com/questions/119649/avoid-unwanted-path-in-zip-file

Solution 5 - Linux

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 works also for a directory.

Solution 6 - Linux

Retain the parent directory so unzip doesn't spew files everywhere

When zipping directories, keeping the parent directory in the archive will help to avoid littering your current directory when you later unzip the archive file

So to avoid retaining all paths, and since you can't use -j and -r together ( you'll get an error ), you can do this instead:

cd path/to/parent/dir/;
zip -r ../my.zip "../$(basename "$PWD")"
cd -;

The "../$(basename "$PWD")" is the magic that retains the parent directory.

So now unzip my.zip will give a folder containing all your files:

parent-directory
├── file1
├── file2
├── dir1
│   ├── file3
│   ├── file4

Instead of littering the current directory with the unzipped files:

file1
file2
dir1
├── file3
├── file4

Solution 7 - Linux

Just use the -jrm option to remove the file and directory structures

zip -jrm /path/to/file.zip /path/to/file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakeView Question on Stackoverflow
Solution 1 - LinuxLars KotthoffView Answer on Stackoverflow
Solution 2 - LinuxVikas TawniyaView Answer on Stackoverflow
Solution 3 - LinuxDan D.View Answer on Stackoverflow
Solution 4 - LinuxflakyView Answer on Stackoverflow
Solution 5 - LinuxMirkoBanchiView Answer on Stackoverflow
Solution 6 - LinuxAndrewDView Answer on Stackoverflow
Solution 7 - LinuxQader DorostiView Answer on Stackoverflow