bash gzip 压缩一组目录并创建一个 tar 压缩文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3341131/
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
gzipping up a set of directories and creating a tar compressed file
提问by morpheous
My bash fu is not what it should be.
我的 bash fu 不是它应该的样子。
I want to create a little batch script which will copy a list of directories into a new zip file.
我想创建一个小的批处理脚本,它将目录列表复制到一个新的 zip 文件中。
There are (at least) two ways I can think of proving the list of files:
我可以(至少)有两种方法来证明文件列表:
read from a file (say config.txt). The file will contain the list of directories to zip up OR
hard code the list directly into the bash script
从文件中读取(比如 config.txt)。该文件将包含要压缩的目录列表或
将列表直接硬编码到 bash 脚本中
The first option seems more straightforward (though less elegant).
第一个选项似乎更直接(虽然不太优雅)。
The two problems I am facing are that I am not sure how to do the following:
我面临的两个问题是我不确定如何执行以下操作:
- provide the list of directories to the shell script
- iterate over the list of directories
- 向 shell 脚本提供目录列表
- 遍历目录列表
Could someone suggest in a few lines, how I can do this?
有人可以建议几行,我该怎么做?
BTW, I am running on Ubuntu 10.0.4
顺便说一句,我在 Ubuntu 10.0.4 上运行
回答by Peter van der Heijden
You can create a gzipped tar on the commandline as follows:
您可以在命令行上创建一个 gzip 压缩包,如下所示:
tar czvf mytar.tar.gz dir1 dir2 .. dirN
If you wanted to do that in a bash script and pass the directories as arguments to the script, those arguments would end up in $@
. So then you have:
如果您想在 bash 脚本中执行此操作并将目录作为参数传递给脚本,则这些参数将以$@
. 那么你有:
tar czvf mytar.tar.gz "$@"
If that is in a script (lets say myscript.sh
), you would call that as:
如果这是在脚本中(可以说myscript.sh
),您可以将其称为:
./myscript.sh dir1 dir2 .. dirN
If you want to read from a list (your option 1) you could do that like so (this does not work if there is whitespace in directory names):
如果您想从列表中读取(您的选项 1),您可以这样做(如果目录名称中有空格,则这不起作用):
tar czvf mytar.tar.gz $(<config.txt)
回答by Simpl
create two files: filelist - place all required directories ( one on single line )
创建两个文件:filelist - 放置所有必需的目录(一个在一行上)
and create a simple bash script:
并创建一个简单的 bash 脚本:
#!/bin/bash
for DIR in `cat filelist`
do
if [ -d $DIR ]
then
echo $DIR
fi
done
回答by Raghuram
You can export a variable like DIRECTORIES="DIR1 DIR2 DIR3 ...." And in the script you need to use the variable like tar czvf $DIRECTORIES
您可以导出像 DIRECTORIES="DIR1 DIR2 DIR3 ...." 这样的变量,并且在脚本中您需要使用像 tar czvf $DIRECTORIES 这样的变量
回答by HymanIT
Just use the null-byte as delimiter when you write file / directory names to file. This way you need not worry about spaces, newlines, etc. in file names!
将文件/目录名称写入文件时,只需使用空字节作为分隔符。这样您就不必担心文件名中的空格、换行符等问题!
printf "%star cf <outputfile_name> --use-compress-prog=pbzip2 <directory_name>
0" */ > listOfDirs.txt # alternative: find ... -print0
while IFS="" read -r -d '' dir; do command ls -1abd "$dir"; done < listOfDirs.txt
tar --null -czvf mytar.tar.gz --files-from listOfDirs.txt
回答by MANAUWER RAZA
In case, you are looking for compressing a directory, the following command can help.
如果您正在寻找压缩目录,以下命令可以提供帮助。
pbzip2 compresses directories using parallel implementation
##代码##pbzip2 使用并行实现压缩目录