find工具的了解与使用1

find的简介

linux中find工具其实非常强大, 平时用到的可能仅仅是它功能的很小的一部分. 最近稍微研究了一下它的用法.

find - search for files in a directory hierarchy

find 搜索目录树上的每一个文件名,它从左至右运算给定的表达式,按照优先规则(见运 算符OPERATORS一节)进行匹配,直到得出结果(左边运算在 ‘与’ 操作中得出假, 在’或’ 操作中得出真),然后 find 移向下一个文件名。

第一个以 ‘-’ , ‘(’ , ‘)’ , ‘,’ 或 ‘!’ 这些字符起始的参数是表达式的开始; 在它之前的任何参数是要搜索的路径,在它之后的任何参数都是表达式的余下部分。 如果没有路径参数,缺省用当前目录。如果没有表达式,缺省表达式 用 ‘-print’.

当所有文件都成功处理时 find 退出并返回状态值0。如果发生错误则返回一个大于0的值。

synopsis(命令语法, 摘要, 或者说命令的套用句型)

在man 1 find中,可以看到find是这样的synopsis

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

今天主要研究find常用的, 不考虑一些细节的选项与用法, 因此本文只谈这样的:
find [starting-point...] [expression]

find的命令句型就是:
find   starting-point   expression  
记住这个, 很多就好理解了.

starting-point说明

starting-point指起始点, 也就是find查找文件时从哪个起始的路径开始查找

比如
$ find / -name foo #从 根目录开始查找文件名为foo的文件
$ find /root/ -name foo # 从/root目录开始查找 名为foo的文件
当没有指定starting-point时, find就认为是"." (点, 即当前目录)

另外, starting-point可以指定多个
$ find / /root /etc -name foo.txt #从/, /root, /etc目录开始查找名字为foo.txt的文件

expression说明

EXPRESSION
The part of the command line after the list of starting points is the expression. This is a kind of query specification describing how we match files and what we do with the files that were matched. An expression is composed of a sequence of things: Tests, Actions, Operators
在starting-point列表后面的, 就是expression, expression主要由 Tests, Actions, Operators 三部分组成.

Tests
Tests return a true or false value, usually on the basis of some property of a file we are considering. The -empty test for example is true only when the current file is empty.

Actions
Actions have side effects (such as printing something on the standard output) and return either true or false, usually based on whether or not they are successful. The -print action for example prints the name of the current file on the standard output.

Operators
Operators join together the other items within the expression. They include for example -o (meaning logical OR) and -a (meaning logical AND). Where an operator is missing, -a is assumed.

If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true.

常见的Tests举例

-name; -perm; -type; -path; -size; 等

常见的Actions举例

-prune; -print; -exec; -delete;

常见的Operators举例

-o; -or; -a; -and; !; -not; 等
运算优先级:
! (-not) 高于 -a(-and) 高于 -o(-or)

find的举例及说明

$ find . -name foo.txt
含义: 在当前目录下, 查找名为foo.txt的文件.
这里面 . 是starting-point, -name foo.txt是Tests, 这条命令中没有Actions, 因此默认Actions就是-print. 即这条命令等同于
$ find . -name foo.txt -print 或
$ find . -name foo.txt -a -print

$ find /tmp -name core -type f -print
含义: 在/tmp目录中, 查找名为core, 同时类型为普通的文件.
这条命令中, /tmp是starting-point, -name core是Tests, -type f也是Tests, -print是Actions. 两个Tests之间没有任何Operator, 因此默认就是-a, 即这条命令等效于
$ find /tmp -name core -a -type f -print 或
$ find /tmp -name core -a -type f -a -print

$ find . -type f -exec file ‘{}’ \;
含义: 对当前目录, 查找类型为普通的文件(regular file), 并对每个文件, 执行 file操作 (file xxx表示 判定xxx是什么文件)
starting-point 是当前目录.
Tests是-type f
Actions是 -exec file ‘{}’ \;

$ find /sbin /usr/sbin -executable ! -readable -print
含义: 找出/sbin, /usr/sbin中, 具有可执行权限, 但没有可读权限的文件, 并打印出来.
Starting-point是 /sbin, /usr/sbin
Tests是 -executable 和 -readable
Operator有两个, 一个是!, 跟 -readable结合, 表示不可读的, 还有一个省略的-a
Actions是-print
这条命令等效于
$ find /sbin /usr/sbin -executable -a ! -readable -print或
$ find /sbin /usr/sbin -executable -a ! -readable -a -print

$ find . -perm 664
含义: Search for files which have read and write permission for their owner, and group, but which other users can read but not write to. Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.
在当前目录中, 找出权限为664 (rw-rw-r–)的文件, 精确匹配.
Starting-point是.
Tests是 -perm 664
Actions是-print (省略了)
这条命令等效于
$ find . -perm 664 -print或
$ find . -perm 664 -a -print

参考

https://www.cnblogs.com/huyuanblog/p/10136286.html
https://jlk.fjfi.cvut.cz/arch/manpages/man/find.1.zh_CN
https://www.gnu.org/software/findutils/manual/html_mono/find.html#Base-Name-Patterns
https://www.gnu.org/software/findutils/manual/html_mono/find.html#Full-Name-Patterns

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值