linux shell 获取某个时间段内的文件

shell脚本里,我们主要用find命令来搜索某类文件,所以在这里,我们也用find来查找时间段内的文件。

主要方法有两种:

一、使用mtime来搜索

这类方法只能精确到天数。

但是一般的需求,也并不需求那么精确的时间,所以还是可以满足大部分需求。

#!/bin/sh 
var=`find . -mtime +10 -a -mtime -20 -type f`
echo "$var"

这个命令是搜索出最近10到20天内修改过的文件。+10表示10天以外,-20表示20天以内。


二、使用newer来搜索

#!/bin/sh 
find . -type f -newermt '2018-04-17 00:00:00' ! -newermt '2018-04-17 23:59:59'
这类方法可以精确到秒。

注意第一个newermt前没有“!”,而第二个newermt前有“!”。


还有,对于以上的两种方法,可以结合我上一篇文章的递归搜索文件夹,把readFile函数修改为:

function readFile() {
    # echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!DIR $1"
    for dirlist in $(ls)
    do
        if test -d ${dirlist}
        then
            cd ${dirlist}
            readFile ${dirlist}
            cd ..
        else
            echo "$dirlist"
            newfile=`echo $dirlist | sed 's/v/___/g'`
            var=`find $dirlist -mtime +10 -a -mtime -20 -type f`
            if [ -n "$var" ]
            then
                mv $dirlist $newfile
            fi
        fi
    done
}
就可以得出,把一个文件夹内的所以有相似特征的一类文件给修改过来。

这是一个非常有用的脚本哦!


最后,还有一个上述mtime和newer这两个参数的详细参考

https://blog.csdn.net/u013654125/article/details/80067249

猜你喜欢

转载自blog.csdn.net/u013654125/article/details/80066914
今日推荐