Monday, May 6, 2024
40
rated 0 times [  40] [ 0]  / answers: 1 / hits: 155135  / 3 Years ago, thu, july 29, 2021, 12:50:11

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?


More From » command-line

 Answers
0

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;

[#23315] Thursday, July 29, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rhaeams

Total Points: 115
Total Questions: 111
Total Answers: 103

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
;