我正在尝试从一个文件创建tar,该文件包含其他文件的列表并将其保存到stdout。
假设有一个名为" files-to-create "的文件。其中包含 /home/abc.txt /home/def.txt 等其他文件的路径,我想创建 abc.txt,def.txt 的tar。
我的脚本包含:
exec 100>&1
tar cf - -T files-to-sync >&100
我正在调用脚本并将其保存到其他文件中,如:
/script.sh > final_tar.tar
但是在创建tar时我遇到错误,有人可以帮助我吗?
答案 0 :(得分:1)
您可以使用以下脚本来实现目标,如果不清楚,请告诉我们:
原型1:
$ cat scriptTar.sh
#!/bin/bash
readonly HELP="$(basename "$0") <list_of_files> <output_tar>
this script will generate a tar file composed of all files present in <list_of_files> input file
the output tar file will be saved as <output_tar>
to run the script provide the input and output filenames"
readonly INPUT_LIST_FILE=$1
readonly OUTPUT_TAR_FILE=$2
if [ -z "$INPUT_LIST_FILE" -o -z "$OUTPUT_TAR_FILE" ]
then
echo $HELP;
exit 1;
fi
tar cf - -T $INPUT_LIST_FILE > $OUTPUT_TAR_FILE
exit $?
文件夹内容:
$ tree .
.
├── a
│ └── abc.txt
├── b
│ └── def.txt
├── c
│ └── ghj.txt
├── files-to-sync.in
└── scriptTar.sh
3 directories, 5 files
列出文件内容:
$ cat files-to-sync.in
./a/abc.txt
./b/def.txt
./c/ghj.txt
<强>执行:强>
$ ./scriptTar.sh files-to-sync.in output.tar
tar文件内容:
$ tar -tvf output.tar
-rw-rw-r-- arobert/arobert 4 2018-02-22 16:50 ./a/abc.txt
-rw-rw-r-- arobert/arobert 4 2018-02-22 16:50 ./b/def.txt
-rw-rw-r-- arobert/arobert 4 2018-02-22 16:50 ./c/ghj.txt
如果您真的想在stdout上显示它,请使用以下脚本:
原型2通过ssh:
#!/bin/bash
readonly HELP="ERROR: $(basename "$0") <list_of_files>
this script will generate to stdout a tar file composed of all files present in <list_of_files> input file
to run the script provide the input file and redirect the output to a file"
readonly INPUT_LIST_FILE=$1
if [ -z "$INPUT_LIST_FILE" ]
then
echo $HELP;
exit 1;
fi
tar cf - -T $INPUT_LIST_FILE
通过ssh执行: $ ssh user @ localhost“cd / home / user / test_tar /; ./scriptTar.sh files-to-sync.in”&gt; output.tar user @ localhost的密码:
生成的tar内容:
tar -tf output.tar
./a/abc.txt
./b/def.txt
./c/ghj.txt
提取内容:
tar xvf output.tar
./a/abc.txt
./b/def.txt
./c/ghj.txt
检查文件:
more ?/*.txt
::::::::::::::
a/abc.txt
::::::::::::::
abc
::::::::::::::
b/def.txt
::::::::::::::
abc
::::::::::::::
c/ghj.txt
::::::
但是,如果我是你,我不仅会生成tar文件,还会添加一些压缩(tar.gz
)并使用rsync
传输文件,以便能够重新启动下载转移错误时停止的位置。
答案 1 :(得分:1)
所以正确的解决方案是
案例1:如果您将文件列表作为参数传递
你可以用这个:
files-to-sync=$1
tar cf - -T files-to-sync
案例2:如果要使用绝对路径作为文件列表
你可以用这个:
tar cfP - -T /path/to/the/file
在绝对路径的情况下使用-P。