Steven Yong
Steven Yong

Reputation: 5446

zip folder in bash

I have the files and folder like the below:

/myproject/config/1.xml
/myproject/src/1.py

I now zip the file like this using bash script:

cd myproject
zip -9 -r -q myfile.zip ./src/*
zip -9 -q myfile.zip ./config/1.xml

The zip file has 2 folders now, 1 config and 1.xml in it and 1 src and 1.py in it. This is what I want, the 2 folders are at the same level in the zip file.

Now I have to move the config folder out a level, src folder remains.

/config/1.xml
/myproject/src/1.py

I can change the codes to the below:

cd myproject
zip -9 -r -q myfile.zip ./src/*
zip -9 -q myfile.zip ../config/1.xml

But the folder config is no longer in the same level as the src.

What am I missing here?

Upvotes: 9

Views: 8819

Answers (1)

Leon
Leon

Reputation: 32514

In order to build a desired hierarchy inside the zipped archive you must pass to zip relative paths representing such a hierarchy. In your case ./src and ../config appear as different levels and this is reflected in the resulting archive. In order to make config and src appear on the same level, you must add an extra cd to your script:

cd /myproject
zip -9 -r -q /myfile.zip ./src/*
cd /
zip -9 -q /myfile.zip ./config/1.xml

Upvotes: 11

Related Questions