我想在 UNIX 中使用 shell 腳本將超過 1 天的日誌移至存檔

我想在 UNIX 中使用 shell 腳本將超過 1 天的日誌移至存檔
cd /home/usr/bin/logs
find . -mtime +1 -print | sed -n -e '/\.\/arc/d' -e 's/.*\//' -e '/*.[0-9]$/p' |  while read i
do
if [-f arc/$i]
then
cat $i >> arc/$i
rm $i
else
mv $i arc
fi
done

日誌檔案有多種格式,例如「valid.app5s.log.1019、app5s.gf3sts.1019、valid.app5s.gf3log.1019、app5s.gf3log.1019、app5s.gf1sts.1019、valid.gf3log.1019、app5s.gf1sts.1019、valid.gf35s.1log app. ,app5s.sts.1019」。

但我無法做到這一點。請幫我解決問題..

答案1

除非我錯過了你可以使用的東西find

find . -maxdepth 1 -mtime +1 -type f -exec mv {} arc/ \;

這將在當前目錄中找到任何早於 1 天的檔案並將其移至“arc”目錄。

答案2

在 find 指令中使用 maxdepth

cd /home/usr/bin/logs
find . -maxdepth 1 -mtime +1 -print |  while read i
do
    if [ -f arc/$i ]
    then
        cat $i >> arc/$i
        rm $i
    else
        mv $i arc
    fi
done

相關內容