35

I want to zip many folders in a directory tree like so

V-
 something.txt
 folder
 folder
 g.jpg
 h.jar

When I try to zip it, it ends creating a zip archive with the v folder instead of the contents of it (the sub directories and files)

How can I avoid this?

25

So if I understand correctly, you are trying to archive the files & folders in a particular folder but without including the root folder.

Example:

/test/test.txt
/test/test2.txt

where test.txt and test2.txt would be stored in the zip, but not /test/

You could cd into the /test/ directory then run something like,

zip -r filename.zip ./*

Which would create an archive in the same folder named filename.zip. Or if running it from outside the folder you could run,

zip -r test.zip test/*

The /* is the part that includes only the contents of the folder, instead of the entire folder.

Edit: OP wanted multiple zips, solution ended up being a bit of a hack, I am curious as to whether there is a better way of doing this.

for d in */ ; do base=$(basename "$d") ; cd $base ; zip -r $base * ; mv "${base}.zip" .. ; cd .. ; done;
| improve this answer | |
  • yes but i have a lot of file which i do the same thing on so it would tedious do it like that so is there like a bash script or something that would automate the same process on the directory that has all the folders – Dami Sep 7 '14 at 1:42
  • 2
    Ok I ran the script and it created a the parent directory in the archive i want only the contents of the folder to be in the archive – Dami Sep 7 '14 at 2:13
  • 1
    my mistake, missed a bit of it. added a -j parameter which i just looked up, apparently skips the part you don't want. try this: for d in */ ; do base=$(basename "$d") ; zip -rj "${base}.zip" "$d" ; done; – zzbomb Sep 7 '14 at 2:19
  • 25
    zip -r test.zip test/* from the outside adds test as directory into the zip file – smihael May 3 '17 at 20:41
  • 4
    as @smihael stated, zip -r test.zip test/* does not work. is there any correct way to achieve this from outside the directory? – andrhamm Aug 7 '17 at 18:56
25

Use the -j or --junk-paths option in your zip command.

From the zip man page:

-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).
| improve this answer | |
  • This appears to be the correct answer. – mutant_city Feb 24 '19 at 17:48
  • 5
    What about nested paths/folders though? – GuyPaddock Mar 13 '19 at 0:25
  • 4
    This will omit all folders in the archive, and if you have duplicate file names in different folders, this will blow up. This has its place, but only if you don't care about folders and just want files. Not what the OP was asking for, but still good knowledge – Govind Rai Apr 3 '19 at 23:26
  • Worked for me, thanks :) – raphaklaus Jan 10 at 12:30
21

How about this command?

$ cd somedir ; zip -r ../zipped.zip . * ; cd ..
| improve this answer | |
  • 6
    While this is a correct answer, the introduction doesn't sound like you're convinced yourself. It would also help to explain a little bit, what the command does. – Nephente Mar 11 '16 at 8:14
  • 1
    This was my solution too, but I dislike it because it involves cd. Was hoping there was a way to specify the stored path more precisely. – nilskp Oct 14 '16 at 16:07
9

Hope this helps.

(cd directory && zip -r ../out.zip .)

It keeps the main shell in the same directory and only changing the directory of the sub-shell which dies after the command.

| improve this answer | |
  • I wanted to add this as a comment to @Alfred UC's answer but I don't have enough reputation for that. – Kenneth Jun 22 '19 at 9:21
  • If it actually solves the problem, then it may be a solution. – ThunderBird Jun 22 '19 at 10:56
  • This is better as an answer. Does this work in both bash and sh? – Arlen Beiler Sep 29 '19 at 20:32
1

The other answers did not satisfy me, because they either included the whole directory structure in the zip, or included an ugly ../../../../file.zip path in the zip command.

The approach below uses a common piece of boilerplate code to get the current directory (the directory in which this script is located) as an absolute path.

#!/bin/bash
dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
(cd "path/to/some/deep/directory/" && zip -r "${dir}/file.zip" ./*)
| improve this answer | |

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.