2017-04-24 35 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

相關問題