bash 脚本根据文件名中的日期查找旧文件

发布于 2024-11-09 05:20:30 字数 317 浏览 0 评论 0原文

我正在开发一个 bash 脚本,该脚本需要根据一个变量搜索单个目录中“旧”的文件,该变量指定在超过阈值之前需要经过多少天,并且文件被标记为要执行操作(可以是任何内容)从移动到存档到删除,等等...)。

问题是文件的修改时间与确定文件需要多久才能采取行动无关,因为文件可能很少更改,脚本的执行时间可能会有所不同,等等

......确定保存的文件是 YYYY-MM-DD 形式的实际文件名(或带有日期命令的 %F)。以文件名contents-2011-05-23.txt为例。可以在此目录中运行哪些命令来查找超过一定天数的所有文件(我当前将阈值设置为 7 天,可以更改)并打印出它们的文件名?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瘫痪情歌 2024-11-16 05:20:30

创建一个 bash 脚本 isOld.sh,如下所示:

#!/bin/bash

fileName=$1
numDays=$2

fileDt=$(echo $fileName | sed 's/^[^-]*-\([^.]*\)\..*$/\1/')
d1=$(date '+%s')
d2=$(date -d $fileDt '+%s')
diff=$((d1-d2))
seconds=$((numDays * 24 * 60 * 60))
[[ diff -ge seconds ]] && echo $fileName

然后通过运行以下命令向上述文件授予执行权限:

chmod +x ./isOld.sh

最后从目录顶部运行此 find 命令以打印超过 7 天的文件,如下所示:

find . -name "contents-*" -exec ./isOld.sh {} 7 \;

Create a bash script isOld.sh like this:

#!/bin/bash

fileName=$1
numDays=$2

fileDt=$(echo $fileName | sed 's/^[^-]*-\([^.]*\)\..*$/\1/')
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 . -name "contents-*" -exec ./isOld.sh {} 7 \;
小红帽 2024-11-16 05:20:30

在BSD中,-j用于防止设置日期,-f参数用于设置输入日期的格式。 :

首先,您需要找到自 1970 年 1 月 1 日以来的天数:

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

现在,您可以使用它来找出 7 天前的时间:

 ((cutoff = $today - 604800))

数字 604800 是 7 天的秒数。

现在,对于目录中的每个文件,您需要找到字符串的日期部分。我不知道有更好的方法。 (也许有人知道一些 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]\).*/\1/')
     yadda, yadda, yadda #Figure this out later
done

一旦我们有了文件日期,我们就可以使用 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 格式的日期:

today=$(date +"%Y-%m-%d)

现在,您可以使用它并找到秒数:

todayInSeconds=(date -d $today +%s)

其他所有内容应该或多或少与上面相同。

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

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

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

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.

聚集的泪 2024-11-16 05:20:30

如果您每天运行该命令,您可以这样做:

echo *-`date -d '8 days ago' '+%F'`.txt

当然可以添加其他通配符

If you run the command daily, you could do this:

echo *-`date -d '8 days ago' '+%F'`.txt

Additional wildcards could be added ofcourse

北方。的韩爷 2024-11-16 05:20:30
find *[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]*.txt -exec bash -c 'dt=`echo $0 | sed -re "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*/\1/"`; file_time=`date -d $dt +%s`; cutoff_time=`date -d "31 days ago" +%s` ; test $file_time -lt $cutoff_time ' {} \; -print

这是我最长的内衬之一:-)这里再次包装:

find *[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]*.txt \
  -exec bash -c ' dt=`echo $0 | \
                  sed -re "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*/\1/"`; \
                  file_time=`date -d $dt +%s`; \
                  cutoff_time=`date -d "31 days ago" +%s` ;\
                  test $file_time -lt $cutoff_time \
                ' {} \; -print
find *[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]*.txt -exec bash -c 'dt=`echo $0 | sed -re "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*/\1/"`; 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:

find *[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]*.txt \
  -exec bash -c ' dt=`echo $0 | \
                  sed -re "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*/\1/"`; \
                  file_time=`date -d $dt +%s`; \
                  cutoff_time=`date -d "31 days ago" +%s` ;\
                  test $file_time -lt $cutoff_time \
                ' {} \; -print
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文