bash bash脚本根据文件名中的日期查找旧文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6099795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash script to find old files based off date in file name
提问by Scott
I'm developing a bash script that needs to search out files within a single directory that are "old" based off a variable that specifies how many days need to pass before the threshold is exceeded and the files are marked for action (could be anything from move to archive to delete, etc...).
我正在开发一个 bash 脚本,它需要根据一个变量在单个目录中搜索“旧”的文件,该变量指定在超过阈值之前需要经过多少天并且文件被标记为操作(可以是任何从移动到存档到删除等...)。
The catch is that the modify time of the file is irrelevant in determining how old the files need to be before taken action upon, as the files may infrequently be changed, the execution time of the script can vary, etc...
问题是文件的修改时间与确定文件在采取行动之前需要多长时间无关,因为文件可能很少更改,脚本的执行时间可能会有所不同等等......
The time that determines hold the files are is in the actual file name in the form of YYYY-MM-DD (or %F with the date command). take for instance the filename contents-2011-05-23.txt. What command(s) could be run in this directory to find all files that exceed a certain amount of days (I have the threshold currently set to 7 days, could change) and print out their file names?
确定保留文件的时间是以 YYYY-MM-DD 形式的实际文件名(或带有日期命令的 %F)。以文件名 contents-2011-05-23.txt 为例。可以在此目录中运行哪些命令来查找超过一定天数的所有文件(我将阈值当前设置为 7 天,可以更改)并打印出它们的文件名?
采纳答案by David W.
In BSD, the -jis used to prevent the date being set and the -fparameter is used to set the format of the input date. :
在BSD中,-j用于防止设置日期,-f参数用于设置输入日期的格式。:
First, you need to find today's date in the number of days since January 1, 1970:
首先,您需要在自 1970 年 1 月 1 日以来的天数中找到今天的日期:
today=$(date -j -f "%Y-%m-%d" 1969-12-31 +%s)
Now, you can use that to find out the time seven days ago:
现在,您可以使用它来找出 7 天前的时间:
((cutoff = $today - 604800))
The number 604800 is the number of seconds in seven days.
数字 604800 是 7 天内的秒数。
Now, for each file in your directory, you need to find the date part of the string. I don't know of a better way. (Maybe someone knows some Bash magic).
现在,对于目录中的每个文件,您需要找到字符串的日期部分。我不知道更好的方法。(也许有人知道一些 Bash 魔法)。
find . -type f | while read fileName
do
fileDate=$(echo $foo | sed 's/.*-\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*//')
yadda, yadda, yadda #Figure this out later
done
Once we have the file date, we can use the date command to figure out if that date in seconds in less than (and thus older than the cutoff date)
一旦我们有了文件日期,我们就可以使用 date 命令来确定该日期是否以秒为单位小于(因此早于截止日期)
today=$(date -j -f "%Y-%m-%d" 1969-12-31 +%s)
((cutoff = $today - 604800))
find . -type f | while read fileName #Or however you get all the file names
do
fileDate=$(echo $foo | sed 's/.*-\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*//')
fileDateInSeconds=$(date -j -f "%Y-%m-%d" $fileDate +%s)
if [ $fileDateInSeconds -lt $cutoff ]
then
rm $fileName
fi
done
In Linux, you use the -dparameter to define the date which must be in YYYY-MM-DDformat:
在 Linux 中,您使用该-d参数来定义必须采用以下YYYY-MM-DD格式的日期:
today=$(date +"%Y-%m-%d)
Now, you can take that and find the number of seconds:
现在,您可以使用它并找到秒数:
todayInSeconds=(date -d $today +%s)
Everything else should be more or less the same as above.
其他一切都应该或多或少与上述相同。
回答by anubhava
Create a bash script isOld.sh like this:
像这样创建一个 bash 脚本 isOld.sh:
#!/bin/bash
fileName=
numDays=
fileDt=$(echo $fileName | sed 's/^[^-]*-\([^.]*\)\..*$//')
d1=$(date '+%s')
d2=$(date -d $fileDt '+%s')
diff=$((d1-d2))
seconds=$((numDays * 24 * 60 * 60))
[[ diff -ge seconds ]] && echo $fileName
Then give execute permission to above file by running:
然后通过运行授予上述文件的执行权限:
chmod +x ./isOld.sh
And finally run this find command from top of your directory to print files older than 7 days as:
最后从目录顶部运行此 find 命令以将超过 7 天的文件打印为:
find . -name "contents-*" -exec ./isOld.sh {} 7 \;
回答by Aaron McDaid
find *[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]*.txt -exec bash -c 'dt=`echo find *[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]*.txt \
-exec bash -c ' dt=`echo echo *-`date -d '8 days ago' '+%F'`.txt
| \
sed -re "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*//"`; \
file_time=`date -d $dt +%s`; \
cutoff_time=`date -d "31 days ago" +%s` ;\
test $file_time -lt $cutoff_time \
' {} \; -print
| sed -re "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*//"`; file_time=`date -d $dt +%s`; cutoff_time=`date -d "31 days ago" +%s` ; test $file_time -lt $cutoff_time ' {} \; -print
That's one of my longest one liners :-) Here it is again wrapped:
那是我最长的内衬之一:-) 这里又包装了:
##代码##回答by Wolph
If you run the command daily, you could do this:
如果你每天运行命令,你可以这样做:
##代码##Additional wildcards could be added ofcourse
当然可以添加额外的通配符

