使用 bash 过滤文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2443078/
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
Filtering Filenames with bash
提问by Stefan
I have a directory full of log files in the form
我有一个充满日志文件的目录
${name}.log.${year}{month}${day}
such that they look like this:
使它们看起来像这样:
logs/
production.log.20100314
production.log.20100321
production.log.20100328
production.log.20100403
production.log.20100410
...
production.log.20100314
production.log.old
I'd like to use a bash script to filter out all the logs older than x amount of month's and dump it into *.log.old
我想使用 bash 脚本过滤掉所有早于 x 个月的日志并将其转储到*.log.old
X=6 #months
LIST=*.log.*;
for file in LIST; do
is_older = file_is_older_than_months( ${file}, ${X} );
if is_older; then
cat ${c} >> production.log.old;
rm ${c};
fi
done;
How can I get all the files older than x months? and... How can I avoid that *.log.oldfile is included in the LIST attribute?
如何获取所有超过 x 个月的文件?和...如何避免 *.log.old文件包含在 LIST 属性中?
回答by mdom
The following script expects GNU date to be installed. You can call it in the directory with your log files with the first parameter as the number of months.
以下脚本需要安装 GNU date。您可以在带有日志文件的目录中使用第一个参数作为月数调用它。
#!/bin/sh
min_date=$(date -d " months ago" "+%Y%m%d")
for log in *.log.*;do
[ "${log%.log.old}" "!=" "$log" ] && continue
[ "${log%.*}.$min_date" "<" "$log" ] && continue
cat "$log" >> "${log%.*}.old"
rm "$log"
done
回答by Paul Creasey
Presumably as a log file, it won't have been modified since it was created?
大概是作为日志文件,自创建以来就不会被修改?
Have you considered something like this...
你有没有考虑过这样的事情......
find ./ -name "*.log.*" -mtime +60 -exec rm {} \;
to delete files that have not been modified for 60 days. If the files have been modified more recently then this is no good of course.
删除 60 天未修改的文件。如果文件最近被修改过,那么这当然不好。
回答by sczizzo
You'll have to compare the logfile date with the current date. Start with the year, multiply by 12 to get the difference in months. Do the same with months, and add them together. This gives you the age of the file in months (according to the file name).
您必须将日志文件日期与当前日期进行比较。从年份开始,乘以 12 以获得以月为单位的差异。对几个月做同样的事情,并将它们加在一起。这会以月为单位为您提供文件的年龄(根据文件名)。
For each filename, you can use an AWK filter to extract the year:
对于每个文件名,您可以使用 AWK 过滤器来提取年份:
awk -F. '{ print substr(,0,4) }'
You also need the current year:
您还需要当前年份:
date "+%Y"
To calculate the difference:
要计算差异:
$(( current_year - file_year ))
Similarly for months.
几个月也是如此。
回答by ghostdog74
assuming you have possibility of modifying the logs and the filename timestamp is the more accurate one. Here's an gawk script.
假设您有可能修改日志并且文件名时间戳是更准确的。这是一个gawk脚本。
#!/bin/bash
awk 'BEGIN{
months=6
current=systime() #get current time in sec
sec=months*30*86400 #months in sec
output="old.production" #output file
}
{
m=split(FILENAME,fn,".")
yr=substr(fn[m],0,4)
mth=substr(fn[m],5,2)
day=substr(fn[m],7,2)
t=mktime(yr" "mth" "day" 00 00 00")
if ( (current-t) > sec){
print "file: "FILENAME" more than "months" month"
while( (getline line < FILENAME )>0 ){
print line > output
}
close(FILENAME)
cmd="rm 7"FILENAME"7"
print cmd
#system(cmd) #uncomment to use
}
}' production*

