I have the following folder structure
folder | |--foo.txt | |--sub_folder | |--bar.txt
I want to zip the content (files and sub folders) of folder without including the root folder in the zip.
I have tried command
zip -r package.zip folder
But this includes the root folder. Also tried the following form
zip -j -r package.zip folder
But this will flatten all the directory structures and just include the files. How do I preserve the internal directory structure but ignore the parent folder?
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
zip stores paths relative to the current directory (when it is invoked), so you need to change that:
(cd folder; zip -r ../package.zip .)
zip -r package.zip folder/*
The above command will zip all files and sub folders under
folder directory (It will ignore the parent directory for folder)
To add to other answers, for scripts, you could use
pushd folder; zip -r ../package.zip .; popd
pushd changes the directory much like
cd, but remembers the previous one, and
popd restores the last remembered directory.
In this case it’s equivalent to removing the parentheses (which spawn a new shell) and adding
; cd .. at the end of the command suggested by Stephen.
Note: I would add this as a comment, but I can’t because I do not have enough reputation.
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂