bash脚本查找旧的文件依据过文件名日期 [英] bash script to find old files based off date in file name

查看:100
本文介绍了bash脚本查找旧的文件依据过文件名日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个需要搜索出一个单一的目录内都基于指定多少天需要的是突破前的阈值和文件被标记行动(传递变量老文件的bash脚本可能从移动任何存档,删除等)。

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...).

美中不足的是该文件的修改时间是在确定文件需要多大的所须采取行动时,因为文件可能很少被改变无关,脚本的执行时间可能有所不同,等等。

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...

这决定持有这些文件的时间是在YYYY-MM-DD(或date命令%F)的形式实际文件名。就拿文件名的内容,2011-05-23.txt。什么命令(S)可以在这个目录中运行发现超过一定数额的天(我有当前设置为7天的门槛,可以改变)的所有文件,并打印出它们的文件名?

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?

推荐答案

在BSD中, -j 用于prevent被设置日期和 -f 参数用来设置输入日期的格式。

In BSD, the -j is used to prevent the date being set and the -f parameter is used to set the format of the input date. :

首先,你需要找到今天的天数日期自1970年1月1日:

First, you need to find today's date in the number of days since January 1, 1970:

 today=$(date -j -f "%Y-%m-%d" 1969-12-31 +%s)

现在,你可以用它来找出时间七天前:

Now, you can use that to find out the time seven days ago:

 ((cutoff = $today - 604800))

数604800是秒七天数。

The number 604800 is the number of seconds in seven days.

现在,在您的每一个文件,你需要找到字符串的日期部分。我不知道一个更好的方式。 (也许有人知道一些猛砸魔法)。

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).

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]\).*/\1/')
     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)

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]\).*/\1/')
     fileDateInSeconds=$(date -j -f "%Y-%m-%d" $fileDate +%s)
     if [ $fileDateInSeconds -lt $cutoff ]
     then
          rm $fileName
     fi
done

在Linux中,使用 -d 参数来定义它必须在 YYYY-MM-DD日期格式:

In Linux, you use the -d parameter to define the date which must be in YYYY-MM-DD format:

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.

这篇关于bash脚本查找旧的文件依据过文件名日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆