bash 如何在bash中压缩不包括绝对路径的目录的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19157852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:09:49  来源:igfitidea点击:

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

linuxbashubuntuzip

提问by thekosmix

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

我有一个包含以下 XML 的文件夹:
/student/class1/roll1.xml
/student/class1/roll2.xml

.
/学生/class1/roll90.xml

I have to create class1.zipin /student. I tried

我必须在 /student 中创建class1.zip。我试过

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

但它会生成一个包含结构的压缩文件:
student/class1/roll1.xml
student/class1/roll2.xml

.
学生/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

我希望它 (class1.zip) 以不包含路径的方式创建,它应该只包含 XML 文件,例如:
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.

一种解决方案是将此脚本 ( zip -r /student/class1.zip /student/class1/) 放在 /student 文件夹中。

Can anyone suggest something else?

任何人都可以提出其他建议吗?

Thanks!

谢谢!

采纳答案by lurker

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 /studentyou could:

如果您有以下几项工作要做,/student您可以:

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