文件查找之find

find是实时查找工具,通过遍历指定路径完成文件查找。

工作特点:

查找速度略慢
精确查找
实时查找
查找条件丰富
可能只搜索用户具备读取和执行权限的目录

格式

find [OPTION]... [查找路径] [查找条件] [处理动作]

查找路径:指定具体目标路径;默认为当前目录

查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件

处理动作:对符合条件的文件做操作,默认输出至屏幕

指定搜索目录层级

-maxdepth level 最大搜索目录深度,指定目录下的文件为第1级
-mindepth level 最小搜索目录深度

范例

[15:28:20 root@Bj-Ubuntu ~]# find /etc -maxdepth 2 -mindepth 2
/etc/rc3.d/S01ssh
/etc/rc3.d/S01lvm2-lvmetad
/etc/rc3.d/S01unattended-upgrades
/etc/rc3.d/S01lvm2-lvmpolld
/etc/rc3.d/S01cron
/etc/rc3.d/S01open-vm-tools
...

对每个目录先处理目录内的文件,再处理目录本身

-depth
-d #warning: the -d option is deprecated; please use -depth instead, because the
latter is a POSIX-compliant feature

范例

[15:45:53 root@Bj-Ubuntu ~]# tree /home/long/
/home/long/
├── f1.txt
├── f2.txt
├── f3.txt
└── test
    ├── f3.txt
    ├── f4.txt
    └── f5.txt

1 directory, 6 files
[15:46:02 root@Bj-Ubuntu ~]# find /home/long/
/home/long/
/home/long/.bash_history
/home/long/.bash_logout
/home/long/f1.txt
/home/long/.gnupg
/home/long/.gnupg/private-keys-v1.d
/home/long/.sudo_as_admin_successful
/home/long/.profile
/home/long/f2.txt
/home/long/f3.txt
/home/long/.cache
/home/long/.cache/motd.legal-displayed
/home/long/.bashrc
/home/long/test
/home/long/test/f4.txt
/home/long/test/f3.txt
/home/long/test/f5.txt
[15:49:08 root@Bj-Ubuntu /opt]# find /opt/test
/opt/test
/opt/test/f4.txt
/opt/test/f6.txt
/opt/test/f5.txt
[15:50:24 root@Bj-Ubuntu ~]# find /opt
/opt
/opt/f1.txt
/opt/f2.txt
/opt/f3.txt
/opt/test
/opt/test/f4.txt
/opt/test/f6.txt
/opt/test/f5.txt
[15:50:29 root@Bj-Ubuntu ~]# find /opt -depth
/opt/f1.txt
/opt/f2.txt
/opt/f3.txt
/opt/test/f4.txt
/opt/test/f6.txt
/opt/test/f5.txt
/opt/test
/opt

根据文件名和inode查找

-name "文件名称" #支持使用glob,如:*, ?, [], [^],通配符要加双引号引起来
-iname "文件名称" #不区分字母大小写
-inum n #按inode号查找
-samefile name #相同inode号的文件
-links n #链接数为n的文件
-regex “PATTERN” #以PATTERN匹配整个文件路径,而非文件名称

范例

[15:51:09 root@Bj-Ubuntu ~]# find /opt -name f1.txt
/opt/f1.txt
[15:53:06 root@Bj-Ubuntu /opt]# find -iname f1.txt
./f1.txt
[15:53:36 root@Bj-Ubuntu /]# find /opt -iname f1.txt
/opt/f1.txt
[15:55:24 root@Bj-Ubuntu /]# find /var -name "log"
/var/log
[15:56:26 root@Bj-Ubuntu /]# find /opt -regex ".*\.txt$"
/opt/f1.txt
/opt/f2.txt
/opt/f3.txt
/opt/test/f4.txt
/opt/test/f6.txt
/opt/test/f5.txt
# 基于正则表达式匹配文件
[15:55:47 root@Bj-Ubuntu /]# find -regex ".*\.txt$"
./lib/firmware/carl9170fw/CMakeLists.txt
./lib/firmware/carl9170fw/config/CMakeLists.txt
./lib/firmware/carl9170fw/minifw/CMakeLists.txt
./lib/firmware/carl9170fw/carlfw/CMakeLists.txt
...

根据属猪、属组查找

-user USERNAME #查找属主为指定用户(UID)的文件
-group GRPNAME #查找属组为指定组(GID)的文件
-uid UserID #查找属主为指定的UID号的文件
-gid GroupID #查找属组为指定的GID号的文件
-nouser #查找没有属主的文件
-nogroup #查找没有属组的文件

根据文件类型查找

-type TYPE
TYPE可以是以下形式:
f: 普通文件
d: 目录文件
l: 符号链接文件
s:套接字文件
b: 块设备文件
c: 字符设备文件
p: 管道文件

范例

#查看/home的目录
[15:56:56 root@Bj-Ubuntu /]# find /home -type d -ls
   131073      4 drwxr-xr-x   4 root     root         4096 Apr 23 20:13 /home
   262444      4 drwx------   5 www      www          4096 Apr 23 20:38 /home/www
   270246      4 drwx------   3 www      www          4096 Apr 23 20:38 /home/www/.gnupg
...

空文件或目录

-empty

范例

[16:00:54 root@Bj-Ubuntu ~]# mkdir /opt/test2 -p
[16:01:16 root@Bj-Ubuntu ~]# 
[16:01:18 root@Bj-Ubuntu ~]# find /opt -type d -empty
/opt/test2

组合条件

与:-a ,默认多个条件是与关系
或:-o
非:-not !

范例

[16:02:50 root@Bj-Ubuntu ~]# find /etc/ -type d -o -type l | wc -l
836
[16:02:57 root@Bj-Ubuntu ~]# find /etc/ -type d -o -type l -ls | wc -l
632
[16:03:37 root@Bj-Ubuntu ~]# find /etc/ \( -type d -o -type l \) -ls | wc -l
836

德·摩根定律

(非 A) 或 (非 B) = 非(A 且 B)
(非 A) 且 (非 B) = 非(A 或 B)

示例:
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)

范例

# 当前路径下找出long用户拥有的文件
[16:10:29 root@Bj-Ubuntu ~]# find . -type f -user long
./a.txt
[16:10:29 root@Bj-Ubuntu ~]# find -user joe -group joe
[16:10:29 root@Bj-Ubuntu ~]# find -user joe -not -group joe
[16:10:29 root@Bj-Ubuntu ~]# find -user joe -o -user jane
[16:10:29 root@Bj-Ubuntu ~]# find -not \( -user joe -o -user jane \)
[16:10:29 root@Bj-Ubuntu ~]# find / -user joe -o -uid 500

范例

[16:10:36 root@Bj-Ubuntu ~]# find ! \( -type d -a -empty \) | wc -l
24
[16:13:59 root@Bj-Ubuntu ~]# find ! -type d -o ! -empty | wc -l
24

# 查找出不是long和www用户的文件
[16:15:34 root@Bj-Ubuntu ~]# find ! -user long ! -user www
.
./host.txt
./.wget-hsts
./.bash_history
[16:15:38 root@Bj-Ubuntu ~]# find ! \( -user long -o -user www \)
.
./host.txt
./.wget-hsts
./.bash_history
[16:17:34 root@Bj-Ubuntu ~]# find ! -user long -a ! -user www
.
./host.txt
./.wget-hsts
./.bash_history
./key.sh
#找出/opt目录下,属主不是root,且文件名不以f开头的文件
[16:24:33 root@Bj-Ubuntu ~]# find /opt \( -not -user root -a -not -name 'f*' \) -ls
  1193537      0 -rw-r--r--   1 long     long            0 Apr 27 15:48 /opt/k7.txt
[16:24:41 root@Bj-Ubuntu ~]# find /opt -not \( -user root -o -name 'f*' \) -ls
  1193537      0 -rw-r--r--   1 long     long            0 Apr 27 15:48 /opt/k7.txt

排除目录

范例

#查找/etc/下,除/etc/security目录的其它所有.conf后缀的文件
[16:26:09 root@Bj-Ubuntu ~]# find /etc -path '/etc/security' -a -prune -o -name "*.conf"
/etc/gai.conf
/etc/request-key.d/id_resolver.conf
/etc/vmware-tools/vgauth.conf
/etc/vmware-tools/tools.conf
...
#查找/etc/下,除/etc/security和/etc/systemd,/etc/dbus-1三个目录的所有.conf后缀的文件
[16:27:32 root@Bj-Ubuntu ~]# find /etc \( -path "/etc/security" -o -path "/etc/systemd" -o -path "/etc/dbus-1" \) -a -prune -o -name "*.conf"
/etc/gai.conf
/etc/request-key.d/id_resolver.conf
/etc/vmware-tools/vgauth.conf
/etc/vmware-tools/tools.conf
...
#排除/proc和/sys目录
[16:31:53 root@Bj-Ubuntu ~]# find / \( -path "/sys" -o -path "/proc" \) -a -prune -o -type f -a -mmin -1

根据文件大小来查找

-size [+|-]#UNIT #常用单位:k, M, G,c(byte),注意大小写敏感
#UNIT: #表示(#-1, #],如:6k 表示(5k,6k]
-#UNIT #表示[0,#-1],如:-6k 表示[0,5k]
+#UNIT #表示(#,∞),如:+6k 表示(6k,∞)

范例

find / -size +10G
[root@centos8 ~]#find / -size +10G
/proc/kcore
find: ‘/proc/25229/task/25229/fd/6’: No such file or directory
find: ‘/proc/25229/task/25229/fdinfo/6’: No such file or directory
find: ‘/proc/25229/fd/5’: No such file or directory
find: ‘/proc/25229/fdinfo/5’: No such file or directory
[root@centos8 ~]#ll -h /proc/kcore
-r-------- 1 root root 128T Dec 14 2020 /proc/kcore
[root@centos8 ~]#du -sh /proc/kcore
0 /proc/kcore

根据时间戳

#以“天”为单位
-atime [+|-]#
# #表示[#,#+1)
+# #表示[#+1,∞]
-# #表示[0,#)
-mtime
-ctime
#以“分钟”为单位
-amin
-mmin
-cmin
访问时间 (-atime/天,-amin/分钟):用户最近一次访问时间。
修改时间 (-mtime/天,-mmin/分钟):文件最后一次修改时间。
变化时间 (-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。

范例

#搜索最近七天内被访问过的所有文件
find . -type f -atime -7
#搜索恰好在七天前被访问过的所有文件
find . -type f -atime 7
#搜索超过七天内被访问过的所有文件
find . -type f -atime +7
#搜索访问时间超过10分钟的所有文件
find . -type f -amin +10
#找出比file.log修改时间更长的所有文件
find . -type f -newer file.log

根据权限查找

-perm [/|-]MODE
MODE #精确权限匹配
/MODE #任何一类(u,g,o)对象的权限中只要能一位匹配即可,或关系,+ 从CentOS 7开始淘汰
-MODE #每一类对象都必须同时拥有指定权限,与关系
0 表示不关注

说明:

find -perm 755 会匹配权限模式恰好是755的文件
只要当任意人有写权限时,find -perm /222就会匹配
只有当每个人都有写权限时,find -perm -222才会匹配
只有当其它人(other)有写权限时,find -perm -002才会匹配

正则表达式

-regextype type
Changes the regular expression syntax understood by -regex and -iregex tests
which occur later on the command line. Currently-implemented types are
emacs (this is the default), posix-awk, posix-basic, posix-egrep and posixextended.
-regex pattern
File name matches regular expression pattern. This is a match on the whole
path, not a search. For example, to match a file named `./fubar3', you can
use the regular expression `.*bar.' or `.*b.*3', but not `f.*r3'. The regular
expressions understood by find are by default Emacs Regular Expressions, but
this can be changed with the -regextype option.

范例

find /you/find/dir -regextype posix-extended -regex "regex"

处理动作

-print:默认的处理动作,显示至屏幕
-ls:类似于对查找到的文件执行"ls -dils"命令格式输出
-fls file:查找到的所有文件的长格式信息保存至指定文件中,相当于 -ls > file
-delete:删除查找到的文件,慎用!
-ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会
交互式要求用户确认
-exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令
{}: 用于引用查找到的文件名称自身

范例

#备份配置文件,添加.bak这个扩展名
[root@centos8 ~]# find -name "*.conf" -exec cp {} {}.bak \;
#提示删除存在时间超过3天以上的joe的临时文件
[root@centos8 ~]# find /tmp -ctime +3 -user joe -ok rm {} \;
#在主目录中寻找可被其它用户写入的文件
[root@centos8 ~]# find ~ -perm -002 -exec chmod o-w {} \;
#查找/data下的权限为644,后缀为sh的普通文件,增加执行权限
[root@centos8 ~]# find /data –type f -perm 644 -name "*.sh" –exec chmod 755 {} \;

参数替换 xargs

由于很多命令不支持管道|来传递参数,xargs用于产生某个命令的参数,xargs 可以读入 stdin 的数
据,并且以空格符或回车符将 stdin 的数据分隔成为参数
另外,许多命令不能接受过多参数,命令执行可能会失败,xargs 可以解决
注意:文件名或者是其他意义的名词内含有空格符的情况

find 经常和 xargs 命令进行组合,形式如下:

find | xargs COMMAND

范例:

#显示10个数字
[root@longwang data]# seq 10 | xargs
1 2 3 4 5 6 7 8 9 10
[root@longwang data]# ls
f1.conf      f1.log   f2.conf.bak  f3.conf      f3.log   f4.conf.bak
f1.conf.bak  f2.conf  f2.log       f3.conf.bak  f4.conf  f4.log
[root@longwang data]# ls | xargs rm
[root@longwang data]# touch f{1..5}.log
[root@longwang data]# touch f{1..5}.txt
[root@longwang data]# find -name "*.log" | xargs ls -Sl
-rw-r--r--. 1 root root 0 Apr 27 16:57 ./f1.log
-rw-r--r--. 1 root root 0 Apr 27 16:57 ./f2.log
-rw-r--r--. 1 root root 0 Apr 27 16:57 ./f3.log
-rw-r--r--. 1 root root 0 Apr 27 16:57 ./f4.log
-rw-r--r--. 1 root root 0 Apr 27 16:57 ./f5.log
[root@longwang data]# echo {1..10} | xargs
1 2 3 4 5 6 7 8 9 10
[root@longwang data]# echo {1..10} | xargs -n1
1
2
3
4
5
6
7
8
9
10
[root@longwang data]# echo {1..10} | xargs -n2
1 2
3 4
5 6
7 8
9 10
#批量创建和删除用户
[root@longwang data]# echo user{1..10} | xargs -n1 useradd
[root@longwang data]# echo user{1..10} | xargs -n1 userdel -r

#这个命令是错误的
find /sbin/ -perm /700 | ls -l
#查找有特殊权限的文件,并排序
find /bin/ -perm /7000 | xargs ls -Sl
#此命令和上面有何区别?
find /bin/ -perm -7000 | xargs ls -Sl
#以字符nul分隔
find -type f -name "*.txt” -print0 | xargs -0 rm
上一篇:Python数据分析:可视化图表注释设置


下一篇:python数据可视化之matplotlib(下)