2017-04-24 55 views
0

我正在尝试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 

} 
+5

尝试'-cvzf'您正在创建一个名为'z'的文件。 – 123

+0

另外,'files =($(find/this/is/my_path/-type f -mtime +3))'不适用于名称中带有空格的文件('some file.txt'将成为两个条目, 'some'和'file.txt')。并且'/ this/is/my_path/*重要* .txt'会将第一个*替换为当前目录中的文件列表,并将* .txt替换为文本文件列表。 –

+1

请参见:[查找文件并将其tar(带空格)](http://stackoverflow.com/q/5891866/3776858) – Cyrus

回答

1
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))示例是倒数第二)。
+0

为什么你会回答然后标记为重复? – 123

+0

[赛勒斯指出了重复]我回答后,指出了(http://stackoverflow.com/questions/43597320/how-can-i-tar-files-in-a-directory-with-bash#comment74245275_43597320)。 –

+0

呃,够公平的,你一定知道它是重复的,尽管...... – 123