Zip an archive without including parent directory
Solution 1
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;
Solution 2
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).
Solution 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.
Solution 4
How about this command?
$ cd somedir ; zip -r ../zipped.zip . * ; cd ..
Solution 5
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)
Dami
Updated on September 18, 2022Comments
-
Dami over 1 year
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?
-
Dami over 9 yearsyes 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
-
jspaetzel over 9 yearsOh, something like this then? (this would create archives of each folder within the current folder)
for d in */ ; do base=$(basename "$d") ; zip -r "${base}.zip" "$d" ; done;
-
Dami over 9 yearsOk 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
-
jspaetzel over 9 yearsmy 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;
-
Dami over 9 yearsOk so i think the -j deflates all the folders in the archive so i have subfolders in the folder i want to archive to it deflates them also an them put them in the root of the archive
-
jspaetzel over 9 yearsThis is a bit of a hack and i feel like there should be a better way to do it... but i think this might do the trick.
for d in */ ; do base=$(basename "$d") ; cd $base ; zip -r $base * ; mv "${base}.zip" .. ; cd .. ; done;
-
Dami over 9 yearsThanks Dude The Hack Worked Now I can Continue With My Work
-
Nephente about 8 yearsWhile 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.
-
nilskp over 7 yearsThis 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. -
smihael about 7 yearszip -r test.zip test/* from the outside adds test as directory into the zip file
-
andrhamm almost 7 yearsas @smihael stated,
zip -r test.zip test/*
does not work. is there any correct way to achieve this from outside the directory? -
Dr. Chocolate about 5 yearsThis appears to be the correct answer.
-
GuyPaddock about 5 yearsWhat about nested paths/folders though?
-
Govind Rai about 5 yearsThis 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
-
Weihang Jian about 5 yearsFor those who want to also archive dot files,
zip -r filename.zip ./*
will not work, please usezip -r filename.zip .
instead. -
Kenneth almost 5 yearsI wanted to add this as a comment to @Alfred UC's answer but I don't have enough reputation for that.
-
ThunderBird almost 5 yearsIf it actually solves the problem, then it may be a solution.
-
Arlen Beiler over 4 yearsThis is better as an answer. Does this work in both bash and sh?
-
Kenneth almost 4 years@ArlenBeiler yes it does. You will need to make sure your installation has
zip
though. -
Ilia Sidorenko about 3 yearsThis answer is wrong, unfortunately. Includes the parent directory, at least for Info-ZIP on mac os.
-
luke simmons almost 3 yearshands down the most elegant one.
-
progonkpa over 2 yearsDoesn't work. @smihael pointed the issue out correctly.
-
progonkpa over 2 yearsOnly thing what worked for me,