今天研究一下find的一些常用的命令。

find格式:find filepath [-option] [-print|-exec|-ok...]

其中常用的option主要有

-type d|f|s|b|c|p
-perm
-user
-group
-mtime
-depth
-newer
-name
-regex

下面我们通过例子来研究,事先准备了这样的一个目录结构

[root@test tmp]# find ./
./
./root2
./root2/sub1
./root2/sub1/sub2
./root
./root/sub1
./root/sub1/sub2
./root/sub1/sub2/sub3
./test.txt

他们的仔细信息如下:

[root@test tmp]# ll -R
.:
total
drwxr-xr-x root root Jul : root
drwxr-xr-x www root Jul : root2
-rw-r--r-- root root Jul : test.txt ./root:
total
drwxrwxrwx root root Jul : sub1 ./root/sub1:
total
drwxr-xr-x root root Jul : sub2 ./root/sub1/sub2:
total
drwxr-xr-x root root Jul : sub3 ./root/sub1/sub2/sub3:
total ./root2:
total
drwxr-xr-x root root Jul : sub1 ./root2/sub1:
total
drwxr-xr-x root root Jul : sub2 ./root2/sub1/sub2:
total

-type: type后面可以接上面说的六种参数,表示查找的类型,其中f d是最常用的,分别表示查找文件和目录。

[root@test tmp]# find ./ -type d
./
./root2
./root2/sub1
./root2/sub1/sub2
./root
./root/sub1
./root/sub1/sub2
./root/sub1/sub2/sub3
[root@test tmp]# find ./ -type f
./test.txt

-perm 权限: perm用于查找某个权限的文件。

[root@test tmp]# find ./ -perm
./root/sub1
[root@test tmp]# find ./ -perm
./
./root2
./root2/sub1
./root2/sub1/sub2
./root
./root/sub1/sub2
./root/sub1/sub2/sub3

-user 用户名: 用于查找特定所有者的文件。

[root@test tmp]# find ./ -user www
./root2
[root@test tmp]# find ./ -user root
./root2/sub1
./root2/sub1/sub2
./root
./root/sub1
./root/sub1/sub2
./root/sub1/sub2/sub3
./test.txt

-group 用户名: 用于查找特定用户组的文件。用法与-user一样。

-mtime -n|+n:-n表示查找n天之内修改过的文件,+n表示查找n天之前修改过的文件。时间轴都以现在的时候为起点。

[root@test tmp]# find ./ -mtime -
.
./root2
./root2/sub1
./root2/sub1/sub2
./root
./root/sub1
./root/sub1/sub2
./root/sub1/sub2/sub3
./test.txt
[root@test tmp]# find ./ -mtime +1
[root@test tmp]#

-newer filepath: 查找比filepath更新的文件

[root@test tmp]# find ./ -newer root2/sub1
./
./test.txt

-depth: 先查找处理目录里的内容,然后再处理目录。

上面的解释可能不太明了,我们来比较下面两个例子:

[root@test tmp]# find root/
root/
root/sub1
root/sub1/sub2
root/sub1/sub2/sub3 [root@test tmp]# find root/ -depth
root/sub1/sub2/sub3
root/sub1/sub2
root/sub1
root/

现在是否明白了,不加-depth时,find先处理当前目录,然后接着处理目录下面的文件/目录。反之,先处理目录下面的内容,再反过来处理目录。

我们再来看上面的例子,加-depth以后,先读取root里的sub1,因为它是目录,于是又先处理了sub1里的sub2,一步一步往下推。等到了sub3以后,因为里面已经没有东西了,所以处理sub3,然后回朔处理sub2,一步一步向上。

-name 文件名: 根据文件名查找。可以使用?代替一个字符,*代替一字串,[]选择一个列表里的字符。

[root@test tmp]# find ./ -name "root?"
./root2
[root@test tmp]# find ./ -name "root*"
./root2
./root
[root@test tmp]# find ./ -name "[rs]*"
./root2
./root2/sub1
./root2/sub1/sub2
./root
./root/sub1
./root/sub1/sub2
./root/sub1/sub2/sub3
[root@test tmp]# find ./ -name "[rst]*"
./root2
./root2/sub1
./root2/sub1/sub2
./root
./root/sub1
./root/sub1/sub2
./root/sub1/sub2/sub3
./test.txt

-regex partern: 这就是牛叉的工具,可以使用正则来匹配文件名。需要注意的是,它要求匹配的是完全的路径。

[root@test tmp]# find ./ -regex 'root'
[root@test tmp]# find ./ -regex './root'
./root
[root@test tmp]# find /tmp/ -regex '/tmp/root'
/tmp/root
[root@test tmp]# find ./ -regex './root.*'
./root2
./root2/sub1
./root2/sub1/sub2
./root
./root/sub1
./root/sub1/sub2
./root/sub1/sub2/sub3

regex使用的正则规则是受-regextype影响的。具体man find看一下吧。

!: 否定前缀。怎么用呢?直接看例子吧

[root@test tmp]# find ./ ! -name sub*
./
./root2
./root
./test.txt [root@test tmp]# find ./ ! -name sub* ! -type d
./test.txt

最新文章

  1. 7-13IN和NOT IN 子查询
  2. 编译生成.NET Core Framework遇到的问题
  3. firefox hack
  4. 关于SharePoint REST中的授权的研究
  5. leetcode 题解: Length of Last Word
  6. Oracle静态数据字典
  7. 《深入Java虚拟机学习笔记》- 第4章 网络移动性
  8. bzoj1028
  9. Android DVM
  10. Python环境以及编辑器
  11. ASP.NET Core学习之二 菜鸟踩坑
  12. C语言——第一次作业
  13. PlantUML + Chrome 联合使用
  14. python闭包和延迟绑定
  15. JavaScript 条件判断算法综合实战
  16. 基于bootstrap模态框的日期选择器
  17. webpack 中的 chunk 种类
  18. TOJ4413: IP address
  19. hdu 4937 base进制只含3456的base数
  20. python解析式

热门文章

  1. 转-MySQL for Mac 安装和基本操作
  2. 455 Assign Cookies 分发饼干
  3. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第五天(非原创)
  4. Spring Cloud是什么?
  5. frame方式布局一段文子,设置宽高
  6. 动态生成表格呈现还是将表格直接绑定gridview等控件呈现的开发方式选择依据
  7. jQuery核心语法
  8. Mysql:is not allowed to connect to this MySQL server [转]
  9. 结对项目--黄金点游戏(邓乐&曾亮)
  10. Python界面编程之六----布局