简体   繁体   English

bash脚本Linux不存在如何将文件压缩到文件夹

[英]How to zip file to a folder doesn't exist with bash script linux

I want to zip a folder to a directory which does not exist. 我想将一个文件夹压缩到一个不存在的目录。 Similar as 类似于

zip -r /home/unExistFolder/test.zip /home/logFolder

If there's no such folder, then create folder. 如果没有这样的文件夹,则创建文件夹。 If there's folder, just put into it. 如果有文件夹,则放入其中。

How can I realize that? 我怎么能知道呢?

 newDir="/path/with spaces/to/archive"

 if ! [[ -d "$newDir" ]] ; then
     mkdir -p "$newDir"
 fi
 zip  -r /home/unExistFolder/test.zip "$newDir"

Some versions of zip may offer an "I'll make the directory if it doesn't exist" feature/option, but if you want something that will always work, this is your best bet. 某些版本的zip可能提供“如果目录不存在,我将创建目录”功能/选项,但是如果您希望某些东西可以一直工作,那么这是最好的选择。

I've made newDir equal to "/path/with spaces/to/archive" to remind you that if you don't dbl-quote your usage of variables, you'll get mysterious error messages. 我将newDir等于“ / path / newDir / to / archive”,以提醒您,如果不对变量的使用加dbl-quote,则会收到神秘的错误消息。 While there are a few occasions where you don't want to dbl-quote a variable, usually you should dbl-quote all variable usage in shell scripting. 尽管在某些情况下,您不希望对变量使用dbl-quote,但通常应在shell脚本中对所有变量的用法使用dbl-quote。

IHTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM