Siddharth Kumar
Siddharth Kumar

Reputation: 1715

How to zip files of a directory excluding absolute path in bash?

I have a folder containing the following XMLs:
/student/class1/roll1.xml
/student/class1/roll2.xml
.
.
/student/class1/roll90.xml

I have to create class1.zip in /student. I tried

zip -r /student/class1.zip /student/class1/

But it results in a zipped file containing structure:
student/class1/roll1.xml
student/class1/roll2.xml
.
.
student/class1/roll90.xml

I want it (class1.zip) to be created in such a way that it doesn't include the path, it should only include the XML files, like:
class1/roll1.xml
class1/roll2.xml
.
.
class1/roll90.xml

One solution is to put this script (zip -r /student/class1.zip /student/class1/) in /student folder.

Can anyone suggest something else?

Thanks!

Upvotes: 3

Views: 7204

Answers (1)

lurker
lurker

Reputation: 58244

Within your script, you should be able to do something like this:

cd /student
zip -r class1.zip class1/

If you have several of these to do under /student you could:

cd /student
for class in class* ; do
    zip -r $class.zip $class
done

Upvotes: 1

Related Questions