linux - 查找指定时间的文件及文件中的关键字

Linux查找某个时间内的文件

1、  n天内修改的(-ctime)
find . -type f -ctime -1| xargs ls –l

说明:

(1) -type f 只搜索文件,不包含文件夹

(2)ctime中的c-change的意思

(3)-ctime +n: n天前修改的;-ctime –n:n天内修改的,修改日期过去n天的

ctime参数指文件日期等状态性参数修改,mtime参数指内容改变:

find . -type f -mtime -1| xargs ls –l

2、n天内访问过的(-actime)
find . -type f -atime -1

说明:

(1)atime中的a-access的意思;

3、  atime、ctime、mtime区别
(1)atime是指access time,即文件被读取或者执行的时间,修改文件是不会改变access time的。网上很多资料都声称cat、more等读取文件的命令会改变atime,但是我试验时却发现使用cat、more时atime没有被修改。这个问题需要另外做研究探讨。

(2)ctime即change time文件状态改变时间,指文件的i结点被修改的时间,如通过chmod修改文件属性,ctime就会被修改。

(3)mtime即modify time,指文件内容被修改的时间。

4、查看文件的时间等属性
[was@dqapp118046 4to5-auth-service]$ stat error.9357.log_file
File: ‘error.9357.log_file’
Size: 2010 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 10304019 Links: 1
Access: (0640/-rw-r-----) Uid: ( 500/ was) Gid: ( 500/ wasgrp)
Access: 2020-05-15 17:02:44.285084683 +0800
Modify: 2020-05-15 17:10:34.770070276 +0800
Change: 2020-05-15 17:10:34.770070276 +0800
Birth: -

要在一个目录中查找2015-12-25创建的java文件,命令:

find -name *.java -newermt '2015-12-25 08:00:00' ! -newermt '2015-12-25 21:00:00'

还有一个方法:

ls -alR --full-time * | grep "2015-12-25"| grep ".java"

这个只能列处文件名,没有路径。
#########################################################################begin----》
应用场景:需要统计多台机器相同目录下相同时间范围内不同文件名的大小。

cd /data1/ballas/default   
files=`find ./ -type f -newermt '2019-01-22 14:30:00' ! -newermt '2019-01-22 14:40:00'|sort`
sum=0
 for i in $files
do
  a=` du $i|awk {'print $1'}`
 sum=$[a+sum]
done
echo $sum

第一行:切换到相关目录
第二行:查找修改时间在2019-01-22 14:30:00到2019-01-22 14:40:00中的文件
第三行至最后:统计每个文件的大小,默认为KB,再sum所有文件的大小

find: 用于查找文件,基本语法如下:

$ find [path] [option] [expression]
1
find ./ -type f -newermt '2019-01-22 14:30:00' ! -newermt '2019-01-22 14:40:00
1
./:指的是当前目录
-type f :指的是查找的类型是普通文件
-newermt:形式是 -newerXY,其中XY均为变量,主要是找到一些X属性比Y属性更新的文件。其中X指代find的目标文件属性,Y代表参照属性。X可选a,c,m;Y可选a,c,m,t。t代表客观绝对时间,只作为参照属性存在,格式为yyyy-MM-dd hh:mm:ss(必须是这个格式如果不是这个格式会报错)。
! -newermt ‘2019-01-22 14:40:00’:表示修改时间低于2019-01-22 14:40:00
find查找的时候,若有多个条件,如下所示,默认多个条件是and,可使用-a代替

find ./ -type f -newermt '2019-01-22 14:30:00' -newermt '2019-01-22 14:40:00
1
find查找的时候,多个条件只需满足其中一个,使用 -o,如下所示

find ./ -type f -newermt '2019-01-22 14:30:00' -o -newermt '2019-01-22 14:40:00
1
find查找的时候,查找不满足条件的,使用 -not,或者用!,如下所示

find ./ -type f -newermt '2019-01-22 14:30:00' -not -newermt '2019-01-22 14:

linux中find查找指定时间段的文件并grep查找内容

find . -type f -newermt '2016-01-01 00:00:00' ! -newermt '2016-02-01 12:00:00' -exec grep aaaa {} ; > tmp.txt

find -newerXY file/time:XY为占位符,a、B、m、c、t分别代表上次访问时间、创建时间、上次modify时间、上次索引节点改变时间和绝对时间;find根据Y的值来计算file的某个时间戳,然后根据X的值来做匹配。t不能做X。

find . -type f -newermt '2020-05-15 00:00:00' ! -newermt '2020-05-15 23:59:59' | xargs grep -r '17388918878' -n
find * -type f -name '.' |xargs grep -r '17388918878' -n

查看日志文件某段时间内的关键字日志:sed -n '/起始时间/,/结束时间/p' 日志文件| grep ‘keyword’

例:sed -n ‘/2018-06-21 14:30:20/,/2018-06-21 16:12:00/p’ catalina.out | grep ‘keyword’

  • 总结

查看当前目录下有多少个文件

ls | wc -w

查看n天内修改的文件

find . -type f -ctime -1 |xargs ls -l

查找目录下某段时间内的文件名

ls -alR --full-time * | grep "2015-12-25"

指定时间生成的文件中查找文件

ls -alR --full-time * |grep '2020-05-15' |grep '*.log'
find -name *.java -newermt '2015-12-25 08:00:00' ! -newermt '2015-12-25 21:00:00'

查找目录下指定修改时间段内的所有文件中的关键字内容

find . -type f -newermt '2020-05-15 00:00:00' ! -newermt '2020-05-15 23:59:59' | xargs grep -r '17388918878' -n

查找某个文件中在某段时间内的关键字内容

ls --full-time |sed -n '/2020-05-15/p'
sed -n ‘/2018-06-21 14:30:20/,/2018-06-21 16:12:00/p’ catalina.out | grep ‘keyword’

查找某个文件中n行到m行内的关键字内容

sed -n 'n,mp' file_name.txt |grep 'xxxx'

猜你喜欢

转载自www.cnblogs.com/sunxiuwen/p/12896271.html
今日推荐