Zip an archive without including parent directory

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?

7 Answers

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

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.

5

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;
12

How about this command?

$ cd somedir ; zip -r ../zipped.zip . * ; cd ..
3
cd `dirname path/to/archive` && zip -rq $OLDPWD/arhive.zip . && cd -

This works not only with flatten tree (like -j) and you can specify any dir (not only children)

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" ./*)
(cd MyDirectory && zip -r - .) >MyArchive.zip

This lets you specify the resulting filename relative to the current directory, rather than relative to the target directory.

As a bonus, this overwrites the archive instead of adding new files to it, which was desired in my case.

Instead of . you can specify files/directories relative to the target directory that you want to include.

Unlike -j it preserves paths relative to the target directory, rather than flattening all files into a single directory.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like