文件查找 locate 和 find

locate 命令

locate 命令依赖于一个数据库文件,系统默认每天会检索一次系统中的所有文件,然后将索引记录到数据库中。
在执行查找时,可直接到数据库中查找记录,所以 locate 比 find 反应更迅速。
在使用 locate 命令查找之前一般需要手动执行 updatedb 命令更新数据库。

locate 的定时任务定义在 /etc/cron.daily/mlocate 文件中。
数据库文件为 /var/lib/mlocate/mlocate.db

locate 是模糊查找。

常用选项

-i, --ignore-case: Ignore case distinctions when matching patterns. 忽略大小写。
--regex: Interpret all PATTERNs as extended regexps. 支持扩展正则。

find 命令

语法

find + 查找路径(默认为当前目录) + 查找条件 + 处理动作(默认为输出到标准输出) 

根据文件名查找

-name [pattern]
-iname [pattern]: Like -name, but the match is case insensitive.

根据文件类型查找

-type

支持的文件类型

f: regular file,普通文件
d: directory,目录文件
l: symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype. 符号链接文件
b:block (buffered) special,块设备文件
c: character (unbuffered) special,字符设备文件
p: named pipe (FIFO),管道文件
s: socket,套接字文件
find /dev -type b -ls     # 查找/dev下的所有块设备并显示信息
find /etc -type l -ls     # 查找/etc下的所有符号链接文件并显示信息

根据文件大小查找

-size n[cwbkMG]

支持的大小单位

'b': for 512-byte blocks (this is the default if no suffix is used)
'c': for bytes
'w': for two-byte words
'k': for Kilobytes (units of 1024 bytes)
'M': for Megabytes (units of 1048576 bytes)
'G': for Gigabytes (units of 1073741824 bytes)

参数n的指定方式

+n     for greater than n
-n     for less than n
n      for exactly n

根据时间查找

以天为单位(24hours)

-atime n: File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.
-ctime n: File's status was last changed n*24 hours ago.
-mtime n: File's data was last modified n*24 hours ago.

以分钟(minutes)为单位

-amin n: File was last accessed n minutes ago.
-cmin n: File's status was last changed n minutes ago.
-mmin n: File's data was last modified n minutes ago.

参数n的指定方式同上

+n     for greater than n
-n     for less than n
n      for exactly n

根据权限查找

-perm mode: File's  permission  bits are exactly mode (octal or symbolic). 精确匹配
-perm -mode: All of the permission bits mode are set for the file. 每一类用户(u,g,o)的权限中的每一位(r,w,x)同时都符合条件
-perm /mode: Any  of the permission bits mode are set for the file. 任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件

权限为0的位置忽略,不参与匹配

find . -perm 644 -ls        查找权限为644的文件;
find . -perm -022 -ls       查找g,o同时拥有w权限的文件;
find . -perm /222 -ls       至少有一类用户有写权限;
find . -perm /111 -ls       至少有一类用户有执行权限;
find . -perm /666 -ls       至少有一类用户有读或者写权限;
find . -perm /002 -ls       其他用户有写权限的文件;

根据所属关系查找

-user uname: File is owned by user uname (numeric user ID allowed). 查找属主为uname的文件
-group gname: File belongs to group gname (numeric group ID allowed). 查找属组为gname的文件
-uid n: File's numeric user ID is n. 查找属主uid为n的文件
-gid n: File's numeric group ID is n. 查找属组gid为n的文件
-nouser: No user corresponds to file's numeric user ID. 查找没有属主的文件
-nogroup: No group corresponds to file's numeric group ID. 查找没有属组的文件

逻辑操作符

与
expr1 expr2
expr1 -a expr2
expr1 -and expr2

或
expr1 -o expr2
expr1 -or expr2

非
! expr
-not expr
查找/var目录下属主为root,且属组为mail的所有文件或目录
# find /var -user root -a -group mail -ls

查找/usr目录下不属于root,bin或hadoop的所有文件或目录;
# find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls
# find /usr -not \(-user root -o -user bin -o -user hadoop \) -ls

查找/etc目录下最近一周内其内容修改过,且属主不是root也不是hadoop用户的文件或目录;
# find /etc -mtime -7 -a -not -user root -a not -user hadoop -ls
# find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls

查找当前系统上没有属主或属组,且最近一周内曾被访问过的文件或目录;
# find / \( -nouser -o -nogroup \) -atime -7 -ls

查找/etc目录下大于1M且类型为普通文件的所有文件;
# find /etc -size +1M -type f -exec ls -lh {} \;

查找/etc目录下所有用户都没有写权限的文件;
# find /etc -not -perm /222 -ls 

查找/etc目录下至少有一类用户没有执行权限的文件;
# find /etc -not -perm -111 -ls

查找/etc/init.d目录下,所有用户都有执行权限,且其他用户有写权限的所有文件;
# find /etc/init.d/ -perm -111 -a -perm -002 -ls

处理动作

-print: True; print the full file name on the standard output, followed by a newline. 将查找到的文件打印到标准输出,默认的处理动作。
-delete: Delete  files. 删除查找到的文件。
-ls: True; list current file in ls -dils format on standard output. 按指定格式列出查找到的文件。
-ok: command: Like -exec but ask the user first. 执行命令,但需要用户交互式确认。
-exec command {} \; : Execute command. 直接执行命令。
-exec command {} + : 新版find用法,内置了xargs,效果与find结合xargs使用相同。

{} 用于引用查找到的文件名自身

find / nouser -a nogroup -ok chown root:root {} \;  # 查找没有属主和属组的所有文件,并更改为属于root
find / -name "*.conf" -exec ls -l {} \;  # 列出系统中所有以.conf结尾的文件
find / -name "*.tmp" -exec rm -rf {} \;  # 删除系统中所有临时文件
find / -perm /002 -exec mv {} {}.danger \;

find /path -type f -exec rm '{}' \;
find /path -type f -exec rm '{}' +
find /path -type f | xargs rm -f

xargs 命令

xargs 命令可以通过管道接收标准输入,并将接收到的字符串通过空格分割成多个参数(默认情况下是通过空格分割),然后将参数传递给其后面的命令,作为后面命令的命令行参数。

管道是实现 "将前面的标准输出作为后面的标准输入"
xargs是实现 "将标准输入作为命令的参数"

echo "--help" | cat    -->    cat "--help"
echo "--help" | xargs cat    -->    cat --help
find . -perm -7 -print | xargs chmod o-w
find . -type d -name "*.svn" | xargs rm -rf

参考:

posted @ 2017-06-01 19:10  KeithTt  阅读(521)  评论(0编辑  收藏  举报