Bash 脚本 - 在一个档案中压缩多个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15620420/
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 04:59:10  来源:igfitidea点击:

Bash Script - Compress multiple directories in one archive

bashshell

提问by Michael Garrison

I made a script a while back that would use zipto compress several different user specified directories. The way the script did this is it would read the directories from config.txtand compress each one individually. It just so happens that, for my uses, all of these directories are in the same parent. For instance, I'll have the following directories in my /Users/username/directory:

不久前我制作了一个脚本,zip用于压缩几个不同的用户指定目录。脚本执行此操作的方式是从中读取目录config.txt并单独压缩每个目录。碰巧的是,就我而言,所有这些目录都在同一个父目录中。例如,我的目录中有以下/Users/username/目录:

Desktop
Documents
Pictures

Is there a way to combine these 3 in the same archive?

有没有办法将这 3 个组合在同一个存档中?

For a reference, here is my current script:

作为参考,这是我当前的脚本:

BKUPDATE="/Users/michaelgarrison/Backup/BKUP_"$(date +%Y)-$(date +%m)-$(date +%d)

# Create the Backup directory if it does not exist
mkdir -p $BKUPDATE

# File where directories are specified
CONFIG="config.txt"

while read SOURCE
do
    DESTINATION="/Users/michaelgarrison/"
    OUTPUT=$BKUPDATE"/Backup_"$SOURCE"_"$(date +%Y)-$(date +%m)-$(date +%d)".zip"
    (cd /Users/michaelgarrison/; zip -r $OUTPUT $SOURCE)
done < $CONFIG

回答by Hayden

zip -r backup Desktop/* Documents/* Pictures/*

zip -r backup Desktop/* Documents/* Pictures/*

That would compress all the files under Desktop, Documents and Pictures under a file named backup.zip

这会将桌面、文档和图片下的所有文件压缩到一个名为 backup.zip

The trick of sorts would be reading the list from the config.txt file, presumably a vertical list or array and making it one long string.

排序的技巧是从 config.txt 文件中读取列表,大概是一个垂直列表或数组,并使其成为一个长字符串。

回答by D-E-N

please read the comments and answers, they answered your question in a good way!!

请阅读评论和答案,他们很好地回答了您的问题!!

  • put constant things out of the loop
  • 把恒定的东西排除在外

OUTPUT="/Users/michaelgarrison/"$BKUPDATE"/Backup_"$(date +"%Y-%m-%d")".zip" DESTINATION="/Users/michaelgarrison/" <--------- do you need this? I think you don't

while read SOURCE
do
    zip -r $OUTPUT $SOURCE
done < $CONFIG

OUTPUT="/Users/michaelgarrison/"$BKUPDATE"/Backup_"$(date +"%Y-%m-%d")".zip" DESTINATION="/Users/michaelgarrison/" <------ --- 你需要这个吗?我想你没有

while read SOURCE
do
    zip -r $OUTPUT $SOURCE
done < $CONFIG

If you want to do this more than once, you may can use rsync

如果您想多次执行此操作,可以使用rsync

回答by Michael Garrison

I think I just answered my own question, but instead of deleting I'll leave it here for future reference. What I did was change the output to the same zip file for each pass.

我想我只是回答了我自己的问题,但我不会删除它,而是将其留在这里以供将来参考。我所做的是将每次传递的输出更改为相同的 zip 文件。

What I did was change:

我所做的是改变:

while read SOURCE
do
    DESTINATION="/Users/michaelgarrison/"
    OUTPUT=$BKUPDATE"/Backup_"$SOURCE"_"$(date +%Y)-$(date +%m)-$(date +%d)".zip"
    (cd /Users/michaelgarrison/; zip -r $OUTPUT $SOURCE)
done < $CONFIG

to:

到:

OUTPUT=$BKUPDATE"/Backup_"$(date +%Y)-$(date +%m)-$(date +%d)".zip"
while read SOURCE
do
    DESTINATION="/Users/michaelgarrison/"
    (cd /Users/michaelgarrison/; zip -r $OUTPUT $SOURCE)
done < $CONFIG