2016-01-13 126 views
-1

你好我有腳本的問題,我想刪除7天以前的文件,但我做了錯誤的東西:)並且不起作用。預先感謝您的幫助linux腳本查找x天以前的文件並刪除

#!/bin/bash 

# sudo apt-get install pydf 
#pydf 

# source 
DST="/mnt/share/backup" 

#find TIF 
FINDFILE=*.TIF 


if ! [ -f "$DST/$FINDFILE" ]; then 
    echo "no file in: $DST" 
    find $DST -type f -mtime +7 -name '*.TIF' 
    if ! [ "$?" = "0" ]; then 
     echo "ERROR $DST/" 
     exit 200 
    fi 
else  
    echo "file exist" 
    echo "older than 7 days wiil deleted " 

    find $DST -type f -mtime +7 -name '*.TIF' -execdir rm -- {} \; 
    if ! [ "$?" = "0" ]; then 
     echo "ERROR : $DST" 
     exit 200 
    fi 
fi 

如果dosent文件存在,那麼find $DST -type f -mtime +7 -name '*.TIF'是否存在find $DST -type f -mtime +7 -name '*.TIF' -execdir rm -- {} \;

輸出端子

[email protected]:/home/fm-x2# ./find.sh 
no file in: /mnt/share/backup 
/mnt/share/backup/P1010007.TIF 
/mnt/share/backup/P1010004.TIF 
/mnt/share/backup/P1010003.TIF 
/mnt/share/backup/P1010009.TIF 
/mnt/share/backup/P1010008.TIF 
/mnt/share/backup/P1010005.TIF 
/mnt/share/backup/P1010006.TIF 

[email protected]:/mnt/share/backup# ls -lh 
razem 224M 
-rw-r--r-- 1 fm-x2 fm-x2 82M sty 13 09:37 2016-01-13.backup.tar.bz2 
-rwxr-xr-x 1 fm-x2 fm-x2 21M gru 31 2004 P1010003.TIF 
-rwxr-xr-x 1 fm-x2 fm-x2 21M gru 31 2004 P1010004.TIF 
-rwxr-xr-x 1 fm-x2 fm-x2 21M gru 31 2004 P1010005.TIF 
-rwxr-xr-x 1 fm-x2 fm-x2 21M gru 31 2004 P1010006.TIF 
-rwxr-xr-x 1 fm-x2 fm-x2 21M gru 31 2004 P1010007.TIF 
-rwxr-xr-x 1 fm-x2 fm-x2 21M gru 31 2004 P1010008.TIF 
-rwxr-xr-x 1 fm-x2 fm-x2 21M gru 31 2004 P1010009.TIF 
[email protected]:/mnt/share/backup# 

在剛開始如果沒有TIFF退出錯誤if ! [ -f "$DST/$FINDFILE" ]; then

問題解決了

13線if ! [ "$DST/$FINDFILE" ]; then

[email protected]:/home/fm-x2# ./find.sh 
file exist 
older than 7 days wiil deleted 

[email protected]:/mnt/share/backup# ls -l 
razem 83536 
-rw-r--r-- 1 fm-x2 fm-x2 85538248 sty 13 09:37 2016-01-13.backup.tar.bz2 
+1

'不work',關心擴大這個 ? – 123

+0

是不工作的輸出終端:'no file in:/ mnt/share/backup /mnt/share/backup/P1010007.TIF /mnt/share/backup/P1010004.TIF'並且不刪除文件 –

+0

請發表你的問題中的整個輸出。 – 123

回答

-1

您可以使用查找和管道執行此任務:

find path/to/directory -time +7 | xargs rm