如何使用Bash tar文件中的文件
问题描述:
我正在尝试tar文件超过3天。我回顾了现有的问题creating tar file and naming by current date,但是当我运行脚本时,文件不会被修改。有没有人有一些提示?如何使用Bash tar文件中的文件
# Tar files older than 3 days
files=($(find /this/is/my_path/ -type f -mtime +3))
tar -cvfz backup.tar.gz "${files[@]}"
if [ ! -f ${LOGFILE}.tar.gz ]; then
Error checking
if [ $? -ne 0 ]; then
Process_Error $ERROR_SUM "This file had a problem $FILE!"
fi
fi
}
答
files=()
while IFS= read -r -d '' file; do
files+=("$file")
done < <(find /this/is/my_path/ -type f -mtime +3 -print0)
tar -cvzf backup.tar.gz -- "${files[@]}"
- 如a comment on the question by @123指出的那样,该参数直接以下
-f
是文件名;在上面,那变成了z
。 -
array=($(...))
天生不可靠:它依赖于字符串在IFS中的字符串分割来查找文件名之间的边界。但是,唯一不能出现在路径中的字符是NUL - 并且NUL不能存储在字符串中(IFS的数据类型)。请参阅BashPitfalls #1(目前,files=($(find . -type f))
示例是倒数第二)。
尝试'-cvzf'您正在创建一个名为'z'的文件。 – 123
另外,'files =($(find/this/is/my_path/-type f -mtime +3))'不适用于名称中带有空格的文件('some file.txt'将成为两个条目, 'some'和'file.txt')。并且'/ this/is/my_path/*重要* .txt'会将第一个*替换为当前目录中的文件列表,并将* .txt替换为文本文件列表。 –
请参见:[查找文件并将其tar(带空格)](http://*.com/q/5891866/3776858) – Cyrus