[20210408]使用linux find注意.txt
--//昨天本想清理生产系统trc文件,使用find命令遇到一些问题,引起一些注意通过例子说明。
--//注:测试前我关闭测试数据库,主要想避免一些测试中数据库再产生trc文件。
$ cd /u01/app/oracle/diag/rdbms/book/book/trace
$ find . -name "*.tr?" -print -mtime +1 |wc
2324 2324 94020
$ find . -name "*.tr?" -print -mtime 1 |wc
2324 2324 94020
$ find . -name "*.tr?" -print -mtime -1 |wc
2324 2324 94020
--//不可能-mtime 的三个参数+1 ,1 ,-1 的情况,3个结果一样。
--//删除-print参数。
$ find . -name "*.tr?" -mtime -1 |wc
56 56 1372
$ find . -name "*.tr?" -mtime 1 |wc
1552 1552 74634
$ find . -name "*.tr?" -mtime +1 |wc
634 634 13968
--//实际上你会以为是-print的问题,实际上写成如下:
$ find . -name "*.tr?" -mtime +1 -print |wc
634 634 13968
--//实际上你不写-print,缺省就是print。看了半天文档我也没有搞明白自己错误在哪里。视乎下面内容说明问题。
$ man find
....
EXPRESSIONS
The expression is made up of options (which affect overall operation rather than the processing of a specific
file, and always return true), tests (which return a true or false value), and actions (which have side effects
and return a true or false value), all separated by operators. and is assumed where the operator is omitted.
If the expression contains no actions other than -prune, -print is performed on all files for which the
expression is true.
--//注意最后这一句。总之如果有print最后放在最后或者干脆不写。实际上写成如下也是正确的。
$ find . -name "*.tr?" -prune -mtime +1|wc
634 634 13968
--//总之在使用find加删除操作前注意看一下,-print最好不写.
--//顺便复习一下touch以及find操作。
# touch -d '2020-07-16 16:55:00.000' /tmp/20200716165500
# touch -t '202007161655' /tmp/20200716165500x
# touch -t '202007161655.00' /tmp/20200716165500y
# ls -l /tmp/20200716165500*
-rw-r----- 1 root root 0 2020-07-16 16:55:00 /tmp/20200716165500
-rw-r----- 1 root root 0 2020-07-16 16:55:00 /tmp/20200716165500x
-rw-r----- 1 root root 0 2020-07-16 16:55:00 /tmp/20200716165500y
--//这样可以创建一个时间戳等于'2020-07-16 16:55:00.000'的文件。
# find . "*.tr?" -newer /tmp/20200716165500
--//取反可以使用-not 或者!
# find . "*.tr?" -not -newer /tmp/20200716165500
# find . "*.tr?" ! -newer /tmp/20200716165500
--//find查询特定范围的文件,以前的测试做一个记录。
touch -t '201111140000' /tmp/start_of_20111114
touch -t '201111141200' /tmp/end_of_20111114
find . -newer /tmp/start_of_20111114 ! -newer /tmp/end_of_20111114 -print