簡體   English   中英

如何在沒有find的情況下在linux shell腳本中根據日期查找和刪除文件?

[英]How can I find and delete files based on date in a linux shell script without find?

請注意,我不能在目標環境中使用“找到”

我需要在linux shell腳本中刪除超過7天的所有文件。 就像是:

FILES=./path/to/dir
for f in $FILES
do
  echo "Processing $f file..."
  # take action on each file. $f store current file name
  # perhaps stat each file to get the last modified date and then delete files with date older than today -7 days.

done

我可以用'stat'來做這件事嗎? 我試圖用

find *.gz -mtime +7 -delete

但發現我不能在目標系統上使用find(沒有cron用戶的權限,這不能更改)。 目標系統是Redhat Enterprise。

文件名的格式如下:

gzip> / mnt / target03 / rest-of-path / web / backups / DATABASENAME_ date "+%Y-%m-%d" .gz

這應該工作:

#!/bin/sh

DIR="/path/to/your/files"
now=$(date +%s)
DAYS=30

for file in "$DIR/"*
do
    if [ $(((`stat $file -c '%Y'`) + (86400 * $DAYS))) -lt $now ]
    then
    # process / rm / whatever the file...
    fi
done

一點解釋: stat <file> -c '%Z'給出文件的修改時間,自文件的UNIX紀元以來為秒,而$(date +%s)給出當前的UNIX時間戳。 然后只需要一個簡單的檢查,看看文件的時間戳加上七天的秒數是否大於當前的時間戳。

由於你在文件名中有時間,然后使用它來刪除時間,這是一些代碼:

此腳本獲取自紀元以來的當前時間(以秒為單位),然后計算7天前的時間戳。 然后,對於每個文件,解析文件名並將每個文件名中嵌入的日期轉換為時間戳,然后比較時間戳以確定要刪除的文件。 使用時間戳消除了直接處理日期的所有麻煩(閏年,幾個月不同的日子等)

實際刪除已注釋掉,因此您可以測試代碼。

#funciton to get timestamp X days prior to input timestamp
# arg1 = number of days past input timestamp
# arg2 = timestamp ( e.g. 1324505111 ) seconds past epoch
getTimestampDaysInPast () {
    daysinpast=$1
    seconds=$2
    while [ $daysinpast -gt 0 ] ; do
    daysinpast=`expr $daysinpast - 1`
    seconds=`expr $seconds - 86400`
    done
# make midnight
    mod=`expr $seconds % 86400`
    seconds=`expr $seconds - $mod`
    echo $seconds
} 
# get current time in seconds since epoch
getCurrentTime() {
    echo `date +"%s"`
}

# parse format and convert time to timestamp
# e.g. 2011-12-23 -> 1324505111
# arg1 = filename with date string in format %Y-%m-%d
getFileTimestamp () {
    filename=$1
    date=`echo $filename |  sed "s/[^0-9\-]*\([0-9\-]*\).*/\1/g"`
    ts=`date -d $date | date +"%s"`
    echo $ts
}

########################### MAIN ############################
# Expect directory where files are to be deleted to be first 
# arg on commandline. If not provided then use current working
# directory

FILEDIR=`pwd`
if [ $# -gt 0 ] ; then 
    FILEDIR=$1
fi
cd $FILEDIR

now=`getCurrentTime`
mustBeBefore=`getTimestampDaysInPast 7 $now`
SAVEIFS=$IFS
# need this to loop around spaces with filenames
IFS=$(echo -en "\n\b")
# for safety change this glob to something more restrictive
for f in * ; do 
    filetime=`getFileTimestamp $f`
    echo "$filetime lt $mustBeBefore"
    if [ $filetime -lt $mustBeBefore ] ; then
    # uncomment this when you have tested this on your system
    echo "rm -f $f"
    fi
done
# only need this if you are going to be doing something else
IFS=$SAVEIFS

如果您更喜歡依賴文件名中的日期,則可以使用此例程檢查日期是否早於另一個:

is_older(){
    local dtcmp=`date -d "$1" +%Y%m%d`; shift
    local today=`date -d "$*" +%Y%m%d`
    return `test $((today - dtcmp)) -gt 0`
}

然后你可以遍歷文件名,傳遞'-7天'作為第二個日期:

for filename in *;
do
    dt_file=`echo $filename | grep -o -E '[12][0-9]{3}(-[0-9]{2}){2}'`
    if is_older "$dt_file" -7 days; then
        # rm $filename or whatever
    fi
done

is_older例程中, date -d "-7 days" +%Y%m%d將返回7天前的日期,以數字格式准備進行比較。

DIR=''

now=$(date +%s)

for file in "$DIR/"*
do
echo $(($(stat "$file" -c '%Z') + $((86400 * 7))))
echo "----------"
echo $now

DONE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM