12

I'm trying to compress a directory at

/home/cyrus/sql

And I wanted to change the working directory when zipping the folder:

/ $ zip -b /home/cyrus sql.zip /home/cyrus/sql

But when I check the zip file:

/ $ unzip -l sql.zip

Archive:  sql.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2013-05-30 10:59   home/cyrus/sql/
  1776642  2013-05-23 10:22   home/cyrus/sql/wordpress.sql
---------                     -------
  1776642                     2 files

The root folder should have been sql. What have I done wrong?

10

From man zip:

-b path

 --temp-path path

Use the specified path for the temporary zip archive. For example:

zip -b /tmp stuff *

will put the temporary zip archive in the directory /tmp, copying over stuff.zip to the current directory when done. This option is useful when updating an existing archive and the file system containing this old archive does not have enough space to hold both old and new archives at the same time. It may also be useful when streaming in some cases to avoid the need for data descriptors. Note that using this option may require zip take additional time to copy the archive file when done to the destination file system.

By default zip stores the full path relative to the current directory. If you want your zipfile to have your sql directory as the root, you'll need to run the command from the /home/cyrus directory.

11

If you want to zip the content of a given directory and store the zip file in the current directory, you can write it:

(cd /some/dir && zip -r - dir-there) > file.zip
5

-b specifies where zip should put temporary files, not that it should change directory somewhere. From man zip:

 -b path
      Use the specified path for the temporary  zip  archive.

Try this:

cd /home/cyrus && zip sql.zip sql
  • Or cd /home/cyrus && zip /sql.zip sql if you want the zip file created in the root. – tripleee May 30 '13 at 5:42
2

not sure if I understand this right but -b will not change the root folder. It will put the temporary zip archive in the directory /home/cryus, copying over sql.zip to the current directory when done. This option is only useful when updating an existing archive, and the file system containing this old archive does not have enough space to hold both old and new archives at the same time.

try to use:

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

  • -j might not be right; I would assume the OP wants to keep relative paths, not remove all paths. – tripleee May 30 '13 at 5:41
1

Inspired by Stephane's answer:

(cd /home/cyrus; zip -r sql.zip sql;)

The result:

  1. you have a sql.zip in /home/cyrus/
  2. sql.zip contains sql/ directory and all its content
  3. you initial PWD doesn't change
0

It may be easier to symlink the folder you are trying to zip to the current directory. That way you can stay where you are.

ln -s /path/to/whatever .
zip -r myzip whatever

Them rm the symlink if you desire.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.