find

find

description

通过文件(名称)、属性、时间、所有者查找文件的程序

options

参数 参数说明
-name 搜索文件名,支持正则
-i 忽略大小写搜索,默认对大小写敏感
-size 大小写搜索,大于(+)、小于(-)
路径 默认在本目录下查找
-group 根据所在组查询
-uid 根据uid查询
-type 文件类型查找,d表示目录,f普通文件
-ctime 根据文件创建时间查询
-perm 根据权限查询
-a and,比如大于2M and 小于4M文件(and -a)
-o or,或者
-prune -o 排除搜索目录,举例find / -path "/mnt" -prune -o -name hosts(排除mnt目录)
xargs 找到文件后对文件进行操作,例如删除等。或者配合 grep 搜索文件内容
-exec 找到文件后对文件进行操作,例如删除操作

examples

find /home

把该目录的内容全部列出来

root@LAPTOP-P81QF5GN:/# find /home
/home
/home/wefjack
/home/wefjack/ceshi.md5

find /home -name *md*

root@LAPTOP-P81QF5GN:/# find /home -name *md5*
/home/wefjack/ceshi.md5

find / -path "/mnt" -prune -o -name ceshi*

root@LAPTOP-P81QF5GN:/# find / -path "/mnt" -prune -o -name ceshi*
/home/wefjack/ceshi.md5

不搜索mnt目录

find /home -name *md5 -ls

搜索完成后执行ls命令

root@LAPTOP-P81QF5GN:/# find /home -name *md5 -ls
0 -rw-r--r--   1 root     root            0 Mar  3 15:51 /home/wefjack/ceshi.md5

find /home -name *md5 | xargs ls -l

root@LAPTOP-P81QF5GN:/# find /home -name *md5 | xargs ls -l
-rw-r--r-- 1 root root 0 Mar  3 18:44 /home/wefjack/1/key.md5
-rw-r--r-- 1 root root 0 Mar  3 15:51 /home/wefjack/ceshi.md5

find /tmp -name core -type f -print | xargs /bin/rm -f

  • 或者使用find /tmp -name core -type f -print | xargs rm -f
  • 找到/tmp目录下文件名为core,文件类型为普通文件,然后删除。
root@LAPTOP-P81QF5GN:/# ll /tmp/
total 4
drwxrwxrwt 1 root root 4096 Mar  3 22:12 ./
drwxr-xr-x 1 root root 4096 Feb  3 10:17 ../
-rw-r--r-- 1 root root    0 Mar  3 22:12 core
-rw-r--r-- 1 root root 1428 Mar  3 21:31 passwd
root@LAPTOP-P81QF5GN:/#
root@LAPTOP-P81QF5GN:/#
root@LAPTOP-P81QF5GN:/# find /tmp -name core -type f -print | xargs /bin/rm -f
root@LAPTOP-P81QF5GN:/#
root@LAPTOP-P81QF5GN:/# ll /tmp/
total 3
drwxrwxrwt 1 root root 4096 Mar  3 22:19 ./
drwxr-xr-x 1 root root 4096 Feb  3 10:17 ../
-rw-r--r-- 1 root root 1428 Mar  3 21:31 passwd

find /tmp/ -iname "ip*" -exec rm -rf {} ;

找到文件后删除

[root@rhel tmp]# find /tmp/ -iname "ip*"
/tmp/ip.bz2
/tmp/ip.txt
/tmp/ip2
/tmp/ipguidang02.tar.z

[root@rhel tmp]# find /tmp/ -iname "ip*" -exec rm -rf {} \;
[root@rhel tmp]#

find /tmp/ | xargs grep 172.168

搜索/tmp下包含172.168的文件

[root@rhel tmp]# find /tmp/ | xargs grep 172\.168
grep: /tmp/: Is a directory
grep: /tmp/.X11-unix: Is a directory
grep: /tmp/.X11-unix/X1024: No such device or address
grep: /tmp/.ICE-unix: Is a directory
grep: /tmp/.ICE-unix/1789: No such device or address
grep: /tmp/.esd-0: Is a directory
grep: /tmp/.esd-0/socket: No such device or address
/tmp/ip.txt:172.168.1.1	//包含172.168
/tmp/ip.txt:172.168.1.2	//包含172.168
/tmp/ip.txt:172.168.1.3	//包含172.168
grep: /tmp/vmware-root_940-2689209484: Is a directory
grep: /tmp/.font-unix: Is a directory
grep: /tmp/systemd-private-ec5769598f62487aa1b984aa4f23b69e-colord.service-iisiBh: Is a directory
grep: /tmp/systemd-private-ec5769598f62487aa1b984aa4f23b69e-colord.service-iisiBh/tmp: Is a directory
grep: /tmp/.esd-1000: Is a directory
grep: /tmp/1: Is a directory
Binary file /tmp/1/ip.tar matches
Binary file /tmp/ipguidang.tar matches
grep: /tmp/.XIM-unix: Is a directory
grep: /tmp/.Test-unix: Is a directory
grep: /tmp/systemd-private-ec5769598f62487aa1b984aa4f23b69e-rtkit-daemon.service-02bCJn: Is a directory
grep: /tmp/systemd-private-ec5769598f62487aa1b984aa4f23b69e-rtkit-daemon.service-02bCJn/tmp: Is a directory
grep: /tmp/systemd-private-ec5769598f62487aa1b984aa4f23b69e-ModemManager.service-lLwAsZ: Is a directory
grep: /tmp/systemd-private-ec5769598f62487aa1b984aa4f23b69e-ModemManager.service-lLwAsZ/tmp: Is a directory
grep: /tmp/vmware-root_944-2697139479: Is a directory
grep: /tmp/test: Is a directory
grep: /tmp/2: Is a directory
grep: /tmp/2/test: Is a directory
/tmp/ip2:172.168.1.1	//包含172.168
/tmp/ip2:172.168.1.2	//包含172.168
/tmp/ip2:172.168.1.3	//包含172.168

$ find /tmp -name core -type f -print | xargs /bin/rm -f
$ find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
$ find . -type f -exec file '{}' \;
$ find / \
$ find $HOME -mtime 0
$ find /sbin /usr/sbin -executable \! -readable -print
$ find . -perm 664
$ find . -perm -664
$ find . -perm /222
$ find . -perm /u+w,g+w
$ find . -perm /u=w,g=w
$ find . -perm -g+w,u+w
$ find . -perm -444 -perm /222 \! -perm /111
$ find . -perm -a+r -perm /a+w \! -perm /a+x
$ find . -name .snapshot -prune -o \( \! -name '*~' -print0 \) \
$ find repo/ \
$ find /tmp -type f,d,l
$ find /tmp \( -type f -o -type d -o -type l \)
$ find / -name needle -print -quit
$ find . .. / /tmp /tmp/TRACE compile compile/64/tests/find -maxdepth 0 -printf '[%h][%f]\n'
$ find . -name *.c -print
$ find . -name '*.c' -print
$ find . -name \*.c -print

参考:

  1. man find

  2. https://www.gnu.org/software/findutils/manual/html_node/find_html/Primary-Index.html)

posted @ 2023-03-03 22:34  wefjack  阅读(103)  评论(0)    收藏  举报