find文件查找
基础语法
find [路径] [选项] [表达式] [动作]
find选项
按文件类型查找
-type:
f:可编辑的文件
d:目录
l:软链接文件
b:块设备文件 磁盘,U盘 /dev/sda
c:字符设备文件 终端
s:socket 安全套接字文件
p:管道文件
~~~~~~~~~~~~~~~~~~~
[root@localhost~]
/opt/music.mp3
/opt/disk
/opt/file-01
/opt/file-02
1)
[root@localhost~]
2)
[root@localhost~]
16785599 9848 -rw-r--r-- 1 root root 10082639 Jul 28 2019 /opt/music.mp3
17245565 1024 -rw-r--r-- 1 root root 1048576 Apr 13 22:56 /opt/disk
16777604 0 -rw-r--r-- 1 root root 0 Apr 1 00:00 /opt/file-01
17197678 0 -rw-r--r-- 1 root root 0 Apr 2 00:00 /opt/file-02
17197680 0 -rw-r--r-- 1 root root 0 Apr 3 00:00 /opt/file-03
[root@localhost~]
1
[root@localhost~]
按文件大小查找
-size
-:小于
+:大于
Num:精准但是又不精准的匹配
[root@localhost~]
mv 源文件 目标路径
mv -t 目标路径 源文件 (特殊)
1)
[root@localhost ~]
2)
[root@localhost ~]
(xargs -i:指定数据流的位置,将数据流放入{}中)
按文件名查找
-name:严格区分大小写
-iname:不区分大小写
*:匹配0次或多次
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
按文件时间查找
-atime:文件访问时间查找
-mtime:文件内容创建,修改时间查找
-ctime:文件属性,修改时间查找
Num:查找第N天的文件(不包括今天)
+Num:查找第N天之前的所有文件(不包括今天)
-NUm:查找从今天开始算,n天内的文件
[root@localhost~]
File: ‘/work’
Size: 229 Blocks: 0 IO Block: 4096 directory
Device: 803h/2051d Inode: 33575032 Links: 4
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-04-30 02:14:39.873521151 +0800
Modify: 2022-04-30 02:14:29.528521764 +0800
Change: 2022-04-30 02:14:29.528521764 +0800
Birth: -
[root@localhost opt]
[root@localhost~]
/opt/
/opt/file-23
/opt/file-24
/opt/file-25
/opt/file-26
/opt/file-27
/opt/file-28
/opt/file-29
/opt/file-30
[root@localhost~]
/opt/music.mp3
/opt/disk
/opt/file-01
/opt/file-02
/opt/file-03
/opt/file-04
/opt/file-05
/opt/file-06
/opt/file-07
/opt/file-08
/opt/file-09
/opt/file-10
/opt/file-11
/opt/file-12
/opt/file-13
/opt/file-14
/opt/file-15
/opt/file-16
/opt/file-17
/opt/file-18
/opt/file-19
/opt/file-20
/opt/file-21
[root@localhost~]
/opt/file-22
[root@localhost~]
[root@localhost~]
< rm ... /opt/music.mp3 > ?
[root@localhost~]
-rw-r--r-- 1 root root 0 Apr 25 00:00 file-25
-rw-r--r-- 1 root root 0 Apr 26 00:00 file-26
-rw-r--r-- 1 root root 0 Apr 27 00:00 file-27
-rw-r--r-- 1 root root 0 Apr 28 00:00 file-28
-rw-r--r-- 1 root root 0 Apr 29 00:00 file-29
-rw-r--r-- 1 root root 0 Apr 30 00:00 file-30
按照文件用户和组查找
-user:查找文件的属主
-nouser:文件没有属主用户的
-group:查找文件的属组
-nogroup:文件没有属组的
[root@localhost~] find /var/ -user root -group mail
按权限查找
-perm
[root@localhost~]
33622796 0 -rw-r--r-- 1 root root 0 Mar 23 10:44 /work/oldgirl/123.txt
33578003 4 -rw-r--r-- 1 root root 58 Mar 23 23:12 /work/heiheihei.txt
33578002 4 -rw-r--r-- 1 root root 6 Apr 5 21:37 /work/passwd.txt
即:每个权限位上,只要在数字权限位下,必须满足该权限位上的权限或者包含该权限
[root@localhost~]
33578038 0 lrwxrwxrwx 1 root root 14 Mar 29 21:23 /work/kobe.jpg -> /root/kobe.jpg
33578039 0 lrwxrwxrwx 1 root root 14 Mar 29 21:24 /work/shen -> /root/kobe.jpg
33578041 0 lrwxrwxrwx 1 root root 14 Mar 29 21:28 /work/kobe.txt -> /root/kobe.txt
[root@localhost~]
16778559 0 drwxr-xr-x 2 root root 6 Apr 14 11:22 ./1
33575036 0 drwxr-xr-x 2 root root 6 Apr 14 11:22 ./2
50692650 0 drwxr-xr-x 2 root root 6 Apr 14 11:22 ./3
[root@localhost~]
33574977 4 dr-xr-x--- 27 root root 4096 Apr 30 14:35 ./
33957930 4 -rw-r--r-- 1 root root 18 Dec 29 2013 ./.bash_logout
33574979 4 -rw------- 1 root root 1447 Mar 15 18:32 ./anaconda-ks.cfg
33578007 0 drwxrws-wx 2 root root 6 Apr 12 20:05 ./setgid
[root@localhost~]
33596695 4 -rw-r--r-- 1 root root 1096 Mar 30 22:00 ./test.txt
33578004 4 -rw-r--r-- 1 root root 280 Mar 24 22:21 ./salary.txt
33574990 24 -rw------- 1 root root 22651 Apr 30 09:47 ./.bash_history
~~~~~~~~~~
属主权限位,有一个w或者有一个x就满足条件
属组权限位,有一个x就满足条件
其他用户权限位,有一个x就满足条件
以上三个权限位上有一个满足即可被选中
按深度查找
-maxdepth
[root@localhost ~]
/etc
/etc/grub.d
/etc/terminfo
/etc/skel
/etc/alternatives
/etc/chkconfig.d
[root@localhost~]
/etc
/etc/grub.d
/etc/terminfo
/etc/skel
/etc/alternatives
/etc/chkconfig.d
/etc/rc.d
/etc/rc.d/init.d
/etc/rc.d/rc0.d
/etc/rc.d/rc1.d
/etc/rc.d/rc2.d
/etc/rc.d/rc3.d
/etc/rc.d/rc4.d
/etc/rc.d/rc5.d
/etc/rc.d/rc6.d
find动作
-print:打印查找到的内容到终端上(find命令默认就是打印结果 -print)
-ls:查看文件的详细信息
-delete:删除查找到的文件(bug跟ls类似,ls看不见的,也删除不掉)并且只能删除空目录
[root@localhost ~]
find: cannot delete ‘/run/initramfs/state/etc’: Directory not empty
find: cannot delete ‘/etc’: Directory not empty
find: cannot delete ‘/var/tmp/etc’: Directory not empty
-ok:找到文件后,执行后面的bash命令,询问是否要操作
-ok 系统命令 {} \;
[root@localhost~]
< rm ... /opt/music.mp3 > ?
-exec:找到文件后,执行后面的bash命令
-exec 系统命令 {} \;
[root@localhost~]
[root@localhost~]
且或非 选项
参数 |
作用 |
-a (默认) |
且 、和 |
-o |
或者 |
! |
取反 |
多选项条件组合查找
[root@localhost~]
[root@localhost~]
巩固加深
1.查找/tmp目录下,属主不是root,且文件名不是以f开头的文件
find /tmp ! -user root ! -name 'f*'
find /tmp ! \( -user root -o -name 'f*' \)
/tmp/ZLS
2.查找/var目录下属主为root,且属组为mail的所有文件
find /var/ -user root -group mail
3.查找/var目录下不属于root、oldboy、zls组的所有文件
find /var/ ! \( -group root -o -group oldboy -o -group zls \)
find /var/ ! -group root ! -group oldboy ! -group zls
4.查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
find /var/ -mtime -7 ! -user root ! -user postfix
find /var/ -mtime -7 ! \( -user root -o -user postfix \)
5.查找/etc/下所有大于1M且类型为普通文件的所有文件
find /etc/ -type f -size +1M
6.将/etc中的所有目录(仅目录)复制到/tmp下,目录结构不变
find /etc/ -type d |xargs -i mkdir -p /tmp/{}
find /etc/ -type d |xargs cp -at /tmp
find /etc/ -type d |xargs cp -rt /tmp
find /etc/ -type d |xargs -i cp {} /tmp
7.将/etc目录复制到 /var/tmp,/var/tmp/etc的所有目录权限为777,/var/tmp/etc/目录中所有文件权限为
666
cp -a /etc /var/tmp
find /var/tmp/etc -type d|xargs chmod 777
find /var/tmp/etc -type f|xargs chmod 666
chmod 777 $(find /var/tmp/etc -type d)
chmod 666 $(find /var/tmp/etc -type f)
`` = $()
[root@localhost opt]
total 0 -rw-r--r-- 1 root root 0 Apr 15 00:57 zls.txtwhich cp
[root@localhost opt]
-rwxr-xr-x. 1 root root 155264 Oct 31 2018 /usr/bin/cp
8.创建touch file{1..10}10个文件,保留file9,其他一次全部删除
touch file{1..10}
find ./ ! -name 'file9'|xargs rm -f
rm -f $(find ./ ! -name 'file9')
9.解释如下每条命令的含义
mkdir /root/dir1
在root目录下创建名为dir1的目录
touch /root/dir1/file{1..10}
在/root/dir1下创建file1..file10的10个文件
find /root/dir1 -type f -name 'file5'
查找 /root/dir1 中 文件名为file5的普通文件
find /root/dir1 ! -name 'file5'
查找 /root/dir1 中 文件名为file5的文件
find /root/dir1 -name 'file5' -o -name 'file9
查找 /root/dir1 中 文件名为file5或者文件名为file9的文件
find /root/dir1 -name 'file5' -o -name 'file9' -ls
查找 /root/dir1 中 文件名为file5或者文件名为file9的文件 并列出文件名为file9的文件的详细信息
find /root/dir1 -name 'file5' -o -name 'file9'|xargs ls -l
查找 /root/dir1 中 文件名为file5或者文件名为file9的文件 并列出所有文件的详细信息
find /root/dir1 \( -name 'file5' -o -name 'file9' \) -ls
查找 /root/dir1 中 文件名为file5和文件名为file9的文件并列出详细信息
find /root/dir1 \( -name 'file5' -o -name 'file9' \) -exec rm -rvf {} \;
查找 /root/dir1 中 文件名为file5和文件名为file9的文件 并删除
find /root/dir1 ! \( -name 'file5' -o -name 'file9' \) -exec rm -vf {} \;
查找 /root/dir1 中 文件名不为file5和文件名不为file9的文件 并删除
思维导图

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人