0

Let's say I'm in ~ and I want to zip the contents of the directory ~/folder-to-zip

I can cd into folder-to-zip and type zip -r9 ../zipped.zip * and that produces the result I want.

I wanted to do the same thing without having to cd out of ~. So from ~ I tried zip -r9 zipped.zip folder-to-zip/* but this produced a slightly different zip file. Are these commands not doing the same thing?

Alexander Soare
  • 2,825
  • 3
  • 25
  • 53
  • This might help: [create zip - ignore directory structure](https://stackoverflow.com/q/9710141/3776858) – Cyrus Nov 08 '19 at 16:54

2 Answers2

1

Yes, these commands act differently. When you use zip it will by keep the paths relative to how you ran the command, so running zip from different working directories will affect the result.

Example:

.
└── test
    ├── file1
    ├── file2
    ├── file3
    └── foo
        ├── file4
        └── file5

If I am in the root directory in this example and use the following command:

zip -r test.zip test/*

I get the following result:

$ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2019-11-08 16:51   test/file1
        0  2019-11-08 16:51   test/file2
        0  2019-11-08 16:51   test/file3
        0  2019-11-08 16:53   test/foo/
        0  2019-11-08 16:52   test/foo/file4
        0  2019-11-08 16:53   test/foo/file5
---------                     -------
        0                     6 files

However if I change directory into test, and this command:

cd test
zip -r ../test2.zip *

I get the following zip file:

$ unzip -l test2.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2019-11-08 16:51   file1
        0  2019-11-08 16:51   file2
        0  2019-11-08 16:51   file3
        0  2019-11-08 16:53   foo/
        0  2019-11-08 16:52   foo/file4
        0  2019-11-08 16:53   foo/file5
---------                     -------
        0                     6 files

Aside:

As an aside, if you do not have a directory structure, you can specify -j which will remove all paths and store the files all in the root of the zip file:

# from the root of the example structure
zip -j test3.zip test/*

Produces:

$ unzip -l test3.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2019-11-08 16:51   file1
        0  2019-11-08 16:51   file2
        0  2019-11-08 16:51   file3
        0  2019-11-08 16:52   file4
        0  2019-11-08 16:53   file5
---------                     -------
        0                     5 files
th3ant
  • 328
  • 1
  • 9
1

It's not clear why you do not want to move from ~. Assuming you do not want to worry about getting back to ~ after the zip, you can place cd+zip into sub shell.

(cd ~/folder-to-zip && zip -r9 ../zipped.zip *)

For the specific case that folder-to-zip does not have sub folder, you can try the '-j' (junk-paths), which will flatten the hierarchy

zip -j -r9 zipped.zip -9 folder-to-zip/*

Update 1:

Based on clarification (see comments below), there is generic pattern to to follow, using sub-shell, when the result zip file is relative to the starting folder (e.g., place the output in the original folder).

# Bash Only (using OLDPWD)
(cd /path/to/folder/to/zip && zip -r9 $OLDPWD/zipped.zip *)

# Posix (using initial PWD, instead of OLDPWD)
(HERE=$PWD ; cd /path/to/folder/to/zip && zip -r9 $HERE/zipped.zip *)
dash-o
  • 13,723
  • 1
  • 10
  • 37
  • 1
    Thanks. I ended up doing the sub shell. PS: I didn't want to move directory because it was 4 folders deep and I need to be zipping it each time I make changes to one of the files and want to create a deployment package. So I created a bash script to make it easy. – Alexander Soare Nov 08 '19 at 17:26
  • @AlexanderSoare I've added few examples which might be helpful to you situation. – dash-o Nov 09 '19 at 05:09