Create zip file and ignore directory structure
Asked Answered
W

8

331

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.

Wile answered 14/3, 2012 at 20:56 Comment(2)
Unix zip directory but excluded specific subdirectories, how to exclude directories and file zipping a directory?, How to exclude a directory when zipping files, etc.Eagle
I was thinking this question predated all of those other stack exchanges but alas superuser's post actually is older than this one. If there is something I can do here to help let me know. This must be quite the hot answer since google still drives so much traffic here. :DWile
C
485

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).
Constituent answered 14/3, 2012 at 20:59 Comment(2)
If -j doesn't work for your directory (together with -r) checkout this answerMofette
sometime it just doesn't work... prefer @Mofette link ;) which basically is a pushd popd comboLeader
V
67

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

Vuillard answered 8/3, 2017 at 12:15 Comment(4)
Actually, you might want do the following: cd path/to/parent/dir/ && zip -r ../../../../name.zip ./* && cd -Aegir
@Aegir How does it make any difference ? Rather it will cause you to add no. of parentDir(..) for each directory in the path :|Vuillard
The result is the same, I agree. But in automated scripts that run in many different environments, you usually avoid global paths, because you have no idea what the global path will be. But from the cd path/to/parent/dir/ you can calculate number of double dots ../ easily.Aegir
@Aegir makes senseVuillard
P
38

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).
Petaloid answered 14/3, 2012 at 21:1 Comment(0)
C
21

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

Chryso answered 23/1, 2013 at 9:42 Comment(0)
P
8

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
Prong answered 2/2, 2020 at 1:29 Comment(1)
However in the ZIP archive an additional unwanted parent directory /../ is created, like /../parent/sub/Combine
P
7

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.

Piggyback answered 5/4, 2016 at 13:36 Comment(0)
F
2

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

zip -jrm /path/to/file.zip /path/to/file
Fluxmeter answered 6/4, 2018 at 15:1 Comment(3)
be carefull, -m --move Move the specified files into the zip archive; actually, this deletes the target directories/files after making the specified zip archive. If a directory becomes empty after removal of the files, the directory is also removed. No deletions are done until zip has created the archive without error. This is useful for conserving disk space, but is potentially dangerous so it is recommended to use it in combination with -T to test the archive before removing all input files.Drumfish
The "-m" flag being on this could HUGELY impact people, possibly even give a couple scares, or even worse, loss of data (without, for example, the -T flag as the commenter above mentioned). -1Domela
that's just too dangerous of an answer.Southerland
S
0

If you are looking for a solution to compress all the folders based on some hierarchy of folders you can create a bash script based on the following:

# Function that compress all the folders in a folder
# @param root_folderpath Path to the folder of execution
# @param parent_folderpath Path to parent folder
compress_folders_in_folder(){

    # Get inside the folder
    cd $parent_folderpath

    # Iterate over every folder and zip. Quotes are used to deal with folder names with spaces.
    for current_folder in *; do echo "Compressing $parent_folderpath/$current_folder"; zip -r "$current_folder"".zip" "$current_folder";  done;

    # Go back to the root folder
    cd $root_folderpath
}

# Set the root of the execution to the current folder
root=`$(basename pwd)`

# Compress all the folders inside subfolders levelA/levelB
for levelA in *; do for levelB in "$levelA"/*; do root_folderpath=$root; parent_folderpath=$levelB; compress_folders_in_folder; done; done
Sempach answered 25/3, 2024 at 21:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.