216

I have a directory called folder that looks like this:

folder
      -> root_folder
                    -> some files

I want to zip this directory into zipped_dir, I tried:

zip -r zipped_dir.zip folder/*

But this generates a ZIP that looks like this:

zipped_dir
          -> folder
                   -> root_folder
                                 -> some files

in other words, it's including the directory whose contents I want to zip. How can I exclude this parent directory from the ZIP, without moving anything?

IE I would like this end result:

zipped_dir
          -> root_folder
                        -> some files
Juicy
  • 3,875

7 Answers7

198

Try to use this command (you will get the idea)

cd folder; zip -r ../zipped_dir.zip *

Maybe there is other way, but this is fastest and simplest for me :)

Romeo Ninov
  • 17,484
100

Use -j; for example: zip -r -j zipped_dir.zip folder/*

It won’t zip 'root_folder'.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
A.B.
  • 1,081
  • 1
  • 7
  • 2
17
zip -r -D zipped.zip *

-D instructs zip to not create directories.

don_crissti
  • 82,805
L. Austin
  • 203
  • 1
    doesn't seem to be a valid option in mac os zip – dtech Jul 16 '20 at 08:30
  • 1
    Works in macos. Don't forget that using * will exclude hidden files, so I ended up using .: zip -D -r ../archive.zip . – Justinas Lelys Sep 10 '20 at 13:55
  • This doesn't work for the simple reason that files aren't directories. Zip stores files with full filenames like folder/root_folder/some files/filename.txt (note that the filename contains slashes). -D just prevents directory-file entries like folder/root_folder/some files to be included., but the files in that directory are still included. – Mark Jeronimus Jul 28 '21 at 09:57
15

This is what works for me:

7z a zipped.zip ./rootDir/*

It will create a zip archive with root: any files/directories inside rootDir. e.g

zipped.zip:
     file1.txt
     otherdir/
        file2.txt

Hidden files:

As correctly pointed on @Shiva Wu's comment the above does not include hidden files.

This is also the case for the rest of the answers (at least the ones which preserve directory structure). One solution is to explicitly add the hidden paths wildcard.

# The command below will include (among other) all files and directories starting with a dot.
7z a zipped.zip ./rootDir/* ./rootDir/.[!.]*

OR

# Same as above in one argument
7z a zipped.zip ./rootDir/{*,.[!.]*}

result:

zipped.zip:
     file1.txt
     .hiddenfile1.txt
     .hiddendir/
        file3.txt
     otherdir/
        file2.txt
2

Note that you may get an error (bash: /usr/bin/zip: Argument list too long) when using Romeo Ninov's answer if you are zipping many thousands of files. To avoid this, use a . instead of a *, like so:

cd folder
zip -r ../zipped_folder.zip .
joe
  • 172
  • 7
2

I use -r option, the "zip filename" and the list of items I want to add into it. Refering to --help option we get:

zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]

You can go into folder that contains the files and directories you want to zip in. The run the command:

zip -r filename.zip ./*

The result is a filename.zip containing everything into the directory you ran the command.

Adding a command for hidden files, we can use ls -a from shell put after zip command (already in the directory that contains your desired files):

FILES=$(ls -a)
zip -r filename.zip $FILES

For fish users, just change FILES=$(ls -a) to set FILES (ls -a).

1

Improved answer with all comment suggestions:

# Include parenthesis so cd does not affect your current terminal
(cd folder; zip -r ../folder.zip ./)

Hidden files will be included too!

Eboubaker
  • 191