码迷,mamicode.com
首页 > 系统相关 > 详细

Linux--find

时间:2019-06-11 22:27:59      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:expr   max   txt   file   remove   文件   基本用法   and   pat   

find的基本语法如下:

$ find [path] [option] [expression]

一、基本用法

1.列出当前目录和子目录下的所有文件

这个命令会列出当前目录以及子目录下的所有文件。

$ find .
.
./tmp
./tmp/a
./tmp/a/4.log
./tmp/a/2.log
./tmp/a/5.log
./tmp/a/1.log
./tmp/a/3.log

2. 查找特殊的目录或路径

下面的命令会查找当前目录下 test 文件夹中的文件,默认列出所有文件。

$ find ./test
./test
./test/abc.txt
./test/subdir
./test/subdir/how.php
./test/cool.php
3. 查找特定文件名称

$ find . -name "1.log"
./tmp/a/1.log

#通配符

$ find . -name "*.log"
./tmp/a/4.log
./tmp/a/2.log
./tmp/a/5.log
./tmp/a/1.log
./tmp/a/3.log

#忽略大小写

$ find . -iname "*.LOG"
./tmp/a/4.log
./tmp/a/2.log
./tmp/a/5.log
./tmp/a/1.log
./tmp/a/3.log

4、限制目录查找的深度
$ find ./test -maxdepth 2 -name "*.php"
./test/subdir/how.php
./test/cool.php

$ find ./test -maxdepth 1 -name *.php
./test/cool.php
5. 只查找文件或目录
$ find ./test -name abc*
./test/abc.txt
./test/abc

#文件
$ find ./test -type f -name "abc*"
./test/abc.txt

#目录
$ find ./test -type d -name "abc*"
./test/abc
6、结合命令输出 

-exec command ;| 注意有个分号";"结尾,该action是用于执行给定的命令。如果命令的返回状态码为0则该action返回true。

| command后面的所有内容都被当作command的参数,直到分号";"为止,其中参数部分使用字符串"{}"时,它
| 表示find找到的文件名,即在执行命令时,"{}"会被逐一替换为find到的文件名,"{}"可以出现在参数中的
| 任何位置,只要出现,它都会被文件名替换。 | 注意,分号";"需要转义,即"\;",如有需要,可以将"{}"用引号包围起来

$ find . -type f -exec ls -s {} \; | sort -n 
0 ./tmp/a/1.log
0 ./tmp/a/2.log
0 ./tmp/a/3.log
0 ./tmp/a/4.log
0 ./tmp/a/5.log

$ find . -exec ls -ld {} \;
drwxrwxr-x. 3 hyb92 hyb92 4096 Jun 11 21:24 .
drwxrwxr-x. 3 hyb92 hyb92 4096 Jun 11 21:24 ./tmp
drwxrwxr-x. 2 hyb92 hyb92 4096 Jun 11 21:25 ./tmp/a
-rw-rw-r--. 1 hyb92 hyb92 0 Jun 11 21:25 ./tmp/a/4.log
-rw-rw-r--. 1 hyb92 hyb92 0 Jun 11 21:25 ./tmp/a/2.log
-rw-rw-r--. 1 hyb92 hyb92 0 Jun 11 21:25 ./tmp/a/5.log
-rw-rw-r--. 1 hyb92 hyb92 0 Jun 11 21:25 ./tmp/a/1.log
-rw-rw-r--. 1 hyb92 hyb92 0 Jun 11 21:25 ./tmp/a/3.log

 

$ find /tmp -type f -name "*.txt" -exec rm -f {} \;

$ find /tmp -type d -name "dirToRemove" -exec rm -r -f {} \;

 

【时间戳类条件】

-anewer file:atime比mtime更接近现在的文件。也就是说,文件修改过之后被访问过
-cnewer file:ctime比mtime更接近现在的文件
-newer  file:比给定文件的mtime更接近现在的文件。
-newer[acm]t TIME:atime/ctime/mtime比时间戳TIME更新的文件
-amin  n:文件的atime在范围n分钟内改变过。注意,n可以是(+ -)n,例如-amin +3表示在3分钟以前
-cmin  n:文件的ctime在范围n分钟内改变过
-mmin  n:文件的mtime在范围n分钟内改变过
-atime n:文件的atime在范围24*n小时内改变过
-ctime n:文件的ctime在范围24*n小时内改变过
-mtime n:文件的mtime在范围24*n小时内改变过
-used  n:最近一次ctime改变n天范围内,atime改变过的文件,即atime比ctime晚n天的文件,可以是(+ -)n

 

Linux--find

标签:expr   max   txt   file   remove   文件   基本用法   and   pat   

原文地址:https://www.cnblogs.com/dreamshe92/p/11006336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!