46

I can't find the answer to this for the life of me. Because I am packaging a zip in a specific way for a build process, I don't want to include a folder at all in the resulting zip at the root. For example, if I have this file path:

MyFolder/
    A.png
    B.txt
    C.mp3

And I use either the command:

zip -r -X "MyFolder.zip" MyFolder/*

or

cd MyFolder; zip -r -X "../MyFolder.zip" *

I end up with a zip file that has the root element of MyFolder. What I want is for when I unzip it is to dump all of it right into the directory, like this:

A.png
B.txt
C.mp3

In other words, I don't want MyFolder or any other folder as the root. I read through the whole manual and have tried numerous options and a lot of Google searching, and zip seems to just really want to have a folder at the root.

Thanks!

Eli
  • 4,874
  • 6
  • 41
  • 50
  • 1
    Surprised that the second test doesn't do what you want. I just tested it here and as I would have expected, it does not include "MyFolder" in the paths. – jcaron Jul 03 '14 at 00:44
  • Yeah, if I unzip it unzips a folder called "MyFolder" containing everything, it doesn't dump everything into that directory as desired. This is on Mac OS X. – Eli Jul 03 '14 at 01:32
  • When you zip, or when you run zipinfo on the resulting .zip, does it give you paths including `MyFolder`? I tried it on my Mac (OS X 10.9.3) and it worked as expected. – jcaron Jul 03 '14 at 01:36
  • Are you sure you didn't mix up the resulting zip files? Also remember that running zip on an existing archive adds more files rather than creating a new archive. – jcaron Jul 03 '14 at 01:38
  • 1
    zipinfo looks correct! Sweet monkey balls, the problem was Mac OS X (opening the zip from the Finder using Archive Utility. Using unzip in the command line works as expected. Thanks for the sanity check! – Eli Jul 03 '14 at 17:30
  • I nearly tried it, but didn't, thinking it couldn't find the name of the folder if it wasn't there, but I suppose it uses the name of the zip file as the folder name? – jcaron Jul 03 '14 at 17:32
  • Yeah, it looks that way. – Eli Mar 20 '16 at 20:46

4 Answers4

57

It was Archive Utility's fault (a Mac OS X unzipper app). When I used the unzip command from the command line, it works great.

(cd MyFolder && zip -r -X "../MyFolder.zip" .)
Eli
  • 4,874
  • 6
  • 41
  • 50
  • 4
    I tweaked your solution so you don't have to actually leave the current directory by wrapping your code in `()` and switching the `;` to a `&&` e.g. `(cd MyFolder && zip -r -X ../MyFolder.zip .)` – Samuel Mburu Aug 17 '20 at 16:32
18

Stumbled across this answer but didnt want to have to change in out of directories. I found the -j option useful which adds all files to the root of the zip. Note that its is all files so subdirectory structure will not be preserved.

So with this folder structure:

MyFolder
 - MyFile1
 - MySubFolder
   - MyFile2

And this command:

zip -rj MyFolder.zip MyFolder

You get this:

MyFolder.zip
 - MyFile1
 - MyFile2
Jonny
  • 614
  • 4
  • 12
5

I found the easier way to make an encrypted zip file with the terminal app on mac (mac os) just from the files of your folder.

The command for the terminal

zip -j -e wishedname.zip yourfolder/*

That's it. Enjoy!

*

For more information to zip command in the terminal app

man zip

What -j and -e do?

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

-e
--encrypt
Encrypt  the contents of the zip archive using a password which is entered on the terminal in response to a prompt (this will not be echoed; if standard error is not a tty, zip will  exit with an error).  The password prompt is repeated to save the user from typing errors.
silversurfer
  • 51
  • 1
  • 2
-1

Install zip

sudo apt install zip

use zip

zip -r foo.zip .

You can use the flags -0 (none) to -9 (best) to change compressionrate

Excluding files can be done via the -x flag. From the man-page:

-x files
--exclude files
          Explicitly exclude the specified files, as in:

                 zip -r foo foo -x \*.o

          which  will  include the contents of foo in foo.zip while excluding all the files that end in .o.  The backslash avoids the shell filename substitution, so that the name matching
          is performed by zip at all directory levels.

          Also possible:

                 zip -r foo foo -x@exclude.lst

          which will include the contents of foo in foo.zip while excluding all the files that match the patterns in the file exclude.lst.

          The long option forms of the above are

                 zip -r foo foo --exclude \*.o

          and

                 zip -r foo foo --exclude @exclude.lst

          Multiple patterns can be specified, as in:

                 zip -r foo foo -x \*.o \*.c

          If there is no space between -x and the pattern, just one value is assumed (no list):

                 zip -r foo foo -x\*.o

          See -i for more on include and exclude.
Atif
  • 1