find
是Unix/Linux命令行工具箱最棒的命令之一,工作方式:沿着文件层次结构向下遍历,匹配符合条件的文件,执行相应的操作。
先预览下测试的目录结构
➜ findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
| |____three_1.txt
|____two
| |____two_1.txt
-name
-name
指定了文件名所必须匹配的字符串,可以使用通配符*
匹配某一特征的文件
'*'在shell中是任意字符,同其他正则语言中的 '.'
- 列出当前目录下所有的
.txt
➜ findtest find . -name "*.txt"
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./three/three_1.txt
./two/two_1.txt
- 列出有
two
相关的文件
➜ findtest find . -name 'two*'
./one/two
./one/two/two_2.txt
./two
./two/two_1.txt
这里看到列出的有文件和目录,如果只需要显示文件或者目录怎么办不呢?
-type
-type
可以指定要搜索的文件类型
- 只列出目录
➜ findtest find . -name 'two*' -type d
./one/two
./two
- 只列出文件
➜ findtest find . -name 'two*' -type f
./one/two/two_2.txt
./two/two_1.txt
-type
有多种选项
文件类型 | 类型参数 |
---|---|
普通文件 | f |
目录 | d |
符号链接 | l |
字符设备 | c |
块设备 | b |
套接字 | s |
FIFO | p |
find
在搜索文件只是第一步,通常我们会对搜索到的文件进行某些操作,比如删除,拷贝等
-delete
删除匹配的到的文件
➜ findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
| |____three_1.txt
|____two
| |____two_1.txt
➜ findtest find . -name 'three_1.txt' -delete
➜ findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
|____two
| |____two_1.txt
➜ findtest
前后目录对比发现我们删除了three_1.txt
-exec
-delete
命令只能单一的删除,如果我们希望执行更多的操作只能使用-exec
find
命令可以借助-exec
与其他命令结合。{}代表匹配到的每一项。
- 将匹配到的
.txt
拷贝到three
中
➜ findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
|____two
| |____two_1.txt
➜ findtest find . -name "*.txt" -exec cp {} ./three \;
cp: ./three/1.txt and ./three/1.txt are identical (not copied).
cp: ./three/one_1.txt and ./three/one_1.txt are identical (not copied).
cp: ./three/two_2.txt and ./three/two_2.txt are identical (not copied).
➜ findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
| |____1.txt
| |____one_1.txt
| |____two_1.txt
| |____two_2.txt
|____two
| |____two_1.txt
最后的结果虽然是我们预期的,但是中间有异常提示,这个暂时不用管,等学习了后续的选项就可以避免
cp: ./three/1.txt and ./three/1.txt are identical (not copied).
-path
选项-path
可以使用通配符来匹配文件路径,-name
是匹配的是文件名,-path
则将文件路径作为一个整体进行匹配
- 列出
three
中的.txt
➜ findtest find . -name '*.txt' -path '**/three/**'
./three/1.txt
./three/one_1.txt
./three/two_1.txt
./three/two_2.txt
➜ findtest
!
'!'同其他语言一样,意为‘否定,取反’
- 列出非
three
中的.txt
➜ findtest find . -name '*.txt' ! -path '**/three/**'
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./two/two_1.txt
有了'!'、‘-path’、‘-name’ 就可以解决我们上边的cp异常了
- 删除
three
中的.txt
,还原目录
➜ findtest find . -name '*.txt' -path '**/three/**' -delete
➜ findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
|____two
| |____two_1.txt
➜ findtest
- 将匹配到的
.txt
拷贝到three
中
➜ findtest find . -name '*.txt' ! -path "**/three/**" -exec cp {} ./three \;
➜ findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
| |____1.txt
| |____one_1.txt
| |____two_1.txt
| |____two_2.txt
|____two
| |____two_1.txt
➜ findtest
现在警告已经完全没有了
atime、mtime、ctime
Unix文件系统中的每一个文件都有三种时间戳:
- 访问时间 (atime):用户最近一次访问文件的时间
- 修改时间 (mtime):文件内容最后一次被修改的时间
- 变化时间 (ctime):文件源数据(例如权限)最后一次改变的时间
-atime
mtime
ctime
可作为find
的时间选项,它们可以指定整数值,单位是天。通常前边还带有-
+
,-
表示小于,+
表示大于
- 找出最近一天访问的
.txt
➜ findtest find . -name '*.txt' -atime -1
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./three/1.txt
./three/one_1.txt
./three/two_1.txt
./three/two_2.txt
./two/two_1.txt
- 列出十天前访问的
.txt
➜ findtest find . -name '*.txt' -atime +10
➜ findtest
因为这个目录是我今天创建的,所以没有匹配内容
相应的还有以分钟为单位的时间选项
- amin
- mmin
- cmin
-size
find
的文件大小短选项-size
- 列出小于
1k
的.txt
➜ findtest find . -name '*.txt' -size -1k
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./three/1.txt
./three/one_1.txt
./three/two_1.txt
./three/two_2.txt
./two/two_1.txt
其他的单位:
单位 | 解释 |
---|---|
b | 块(512字节) |
c | 字节 |
w | 字(2字节) |
k | 1024字节 |
M | 1024K字节 |
G | 1024M字节 |
-prune
搜索某些目录是可能需要跳过一些目录,从而提高性能。比如.git
.svn
,将某些目录从搜索路径中排除这种技巧成为修剪
- 列出非
three
中的.txt
➜ findtest find . \( -name "three" -prune \) -o \( -name '*.txt' \)
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./three
./two/two_1.txt
➜ findtest find . ! -path "**/three/**" -name '*.txt'
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./two/two_1.txt
➜ findtest
结束语:常用find
探索世界~~