find基本用法
单一匹配:find [查找目录] [参数] [匹配模型]
多参数匹配:find [查找目录] [参数] [匹配模型] [参数] [匹配模型]
1、find . -name "*.sh"
根据文件名过滤
2、find . -user root
根据用户过滤
3、find . -perm 755
根据权限过滤
4、find . -size +1000000c
根据文件大小过滤
+大于,-小于,c字节 b:block(512字节) w:two-byte words k:kilobytes(1024字节)
5、find ./Desktop/ -type d
根据文件类型
d:目录 l:链接 f:普通文件 s:socket 更多类型见 man find
6、find ./Desktop -type f -newermt '2019-07-01 00:00:00'
查找桌面上2019-07-01以后更新过的文件
-newermt是-newerXY参数的一种,
X可以为aBcm,a:access B:birth c:change m:modify,不是所有系统都支持B参数,慎用
Y一般为t,表示时间
find命令只会显示文件路径,不会显示文件具体信息,ls -l可以显示相对的全路径和具体信息,两者通过xargs结合使用
find ./Desktop -type f -newermt '2019-07-01 00:00:00' -size +1000c | xargs ls -l --time-style '+%Y-%m-%d'
-rw-r--r-- 1 root root 3580 2019-08-01 ./Desktop/a.txt
-rw-r--r-- 1 root root 3900 2019-07-27 ./Desktop/hello-world-1.1-1.el7.x86_64.rpm
-rwxrwxrwx 1 root root 1328 2019-07-24 ./Desktop/testcmd.1
如上显示桌面上2019-07-01以后更新过的大于1000字节的文件
ls也可以换成rm,删除指定时间的文件,
反向过滤
!
find ./Desktop -type f ! -newermt '2019-07-01 00:00:00' -size +1000c | xargs ls -l --time-style '+%Y-%m-%d'
-rw-r--r-- 1 root root 88308 2019-04-13 ./Desktop/5.sql
-rwxrwxrwx 1 root root 1809 2019-04-12 ./Desktop/addd.sh
-rw-r--r-- 1 root root 1074 2019-04-12 ./Desktop/dump.rdb
-rw-rw-r-- 1 king king 1351 2019-05-15 ./Desktop/test.go
-rw-r--r-- 1 root root 100963 2019-06-14 ./Desktop/test.sql
如上显示桌面上在2019-07-01以前最后更新过的大于1000字节的文件
find ./Desktop -type f -newermt '2019-05-01 00:00:00' ! -newermt '2019-07-01' -size +1000c | xargs ls -l --time-style '+%Y-%m-%d'
-rw-rw-r-- 1 king king 1351 2019-05-15 ./Desktop/test.go
-rw-r--r-- 1 root root 100963 2019-06-14 ./Desktop/test.sql
如上显示桌面上在2019-05-01到2019-07-01之间最后更新过的大于1000字节的文件
使用find -exec参数结合其他命令
find ./Desktop -type f -newermt '2019-06-01 00:00:00' ! -newermt '2019-07-01' -size +1000c -exec ls -l {} \;
{}就是一条路径,后面接空格,反斜线,分号,看起来怪怪的,还没xargs好理解
小贴士:
ls使用--time-style自定义时间显示格式
ls -l --time-style long-iso ./Desktop/
ls -l --time-style '+%Y-%m-%d %H:%M:%S.%s' ./Desktop/