find文件查找
基础语法
find [路径] [选项] [表达式] [动作]
find选项
按文件类型查找
# 选项:
-type:
# 文件类型:
f:可编辑的文件
d:目录
l:软链接文件
b:块设备文件 磁盘,U盘 /dev/sda
c:字符设备文件 终端
s:socket 安全套接字文件
p:管道文件
~~~~~~~~~~~~~~~~~~~
## find [路径] [选项]
### 示例:
[root@localhost~]# find /opt/ -type f
/opt/music.mp3
/opt/disk
/opt/file-01
/opt/file-02
### 查看找出文件的详细信息
1)
[root@localhost~]# find /opt/ -type f|xargs ls -l
2)
[root@localhost~]# find /opt/ -type f -ls
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
### 查询/opt/目录下所有目录,总共有多少个?
[root@localhost~]# find /opt/ -type d|wc -l
1
### 使用find找出(/etc下的所有目录)把它们(仅目录)全部复制到/tmp下 只保留目录结构
[root@localhost~]# find /etc/ -type d |xargs -i mkdir -p /tmp/{}
按文件大小查找
# 选项
-size
-:小于
+:大于
Num:精准但是又不精准的匹配
## 示例:
### 使用find找出(/work下小于1k的文件)并删除
[root@localhost~]# find /work/ -size -1M|xargs rm -fvr
### 使用find找出(/opt下大于1M的文件)把它们全部移动到/tmp下
mv 源文件 目标路径
mv -t 目标路径 源文件 (特殊)
1)
[root@localhost ~]# find /opt/ -size +1M |xargs mv -t /tmp
2)
[root@localhost ~]# find /opt/ -size +1M |xargs -i mv {} /tmp
(xargs -i:指定数据流的位置,将数据流放入{}中)
按文件名查找
# 选项
-name:严格区分大小写
-iname:不区分大小写
*:匹配0次或多次
# 精准查找文件名为ceshi的文件
[root@localhost ~]# find / -name 'ceshi'
# 查找文件名以ceshi结尾的和ceshi本身文件
[root@localhost ~]# find / -name '*ceshi'
# 查找文件名以ceshi开头的和ceshi本身文件
[root@localhost ~]# find / -name 'ceshi*'
# 查找文件名包含ceshi的所有文件
[root@localhost ~]# find / -name '*ceshi*'
# 查找文件名为ceshi的所有文件,且不区分大小写
[root@localhost ~]# find / -iname 'ceshi'
按文件时间查找
# 选项
-atime:文件访问时间查找
-mtime:文件内容创建,修改时间查找
-ctime:文件属性,修改时间查找
## 通常情况下用 ( -mtime )文件修改时间来进行查找
Num:查找第N天的文件(不包括今天)
+Num:查找第N天之前的所有文件(不包括今天)
-NUm:查找从今天开始算,n天内的文件
# 查看三种时间
[root@localhost~]# stat /work
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]# for i in `seq -w 30`;do date -s 202204$i && touch file-$i;done
### 查找近八天的文件,包括今天
[root@localhost~]# find /opt/ -mtime -8
/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~]# find /opt/ -mtime +8
/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~]# find /opt/ -mtime 8
/opt/file-22
### 保留最近8天的文件
[root@localhost~]# find /opt/ !-mtime -8 | xargs rm -f
[root@localhost~]# find /opt/ ! -mtime -8 -ok rm -f {} \;
< rm ... /opt/music.mp3 > ?
[root@localhost~]# find /opt/ ! -mtime -8 -esec rm -f {} \;
-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:文件没有属组的
## 示例:
###查找/var目录下属主为root,且属组为mail的所有文件
[root@localhost~] find /var/ -user root -group mail
按权限查找
# 选项:
-perm
## 权限精确查找
[root@localhost~]# find /work -perm 644 -ls
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~]# find /work -perm -622 -ls
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~]# find ./ -perm -611 -ls
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~]# find ./ -perm /611 -ls
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~]# find ./ -perm /311 -ls
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
~~~~~~~~~~
# 如上 /311
属主权限位,有一个w或者有一个x就满足条件
属组权限位,有一个x就满足条件
其他用户权限位,有一个x就满足条件
以上三个权限位上有一个满足即可被选中
按深度查找
# 选项: 针对目录层级查找
-maxdepth
## 查找/etc/目录下的所有1级目录
[root@localhost ~]# find /etc -type d -maxdepth 1
/etc
/etc/grub.d
/etc/terminfo
/etc/skel
/etc/alternatives
/etc/chkconfig.d
## 查找/etc/目录下的所有1级和2级目录目录
[root@localhost~]# find /etc -type d -maxdepth 2
/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:查看文件的详细信息
# 也可以 |xargs ls -l 或者 ls -l $(find xxx) 或者 ls -l `find xxx`
-delete:删除查找到的文件(bug跟ls类似,ls看不见的,也删除不掉)并且只能删除空目录
# 也可以 |xargs rm -fr 或者 rm -fr $(find xxx) 或者 rm -fr `find xxx`
### 示例:
[root@localhost ~]# find / -name 'etc' -delete
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~]# find /opt/ ! -mtime -8 -ok rm -f {} \;
< rm ... /opt/music.mp3 > ?
-exec:找到文件后,执行后面的bash命令
##语法:
-exec 系统命令 {} \;
### 示例:
[root@localhost~]# touch /var/file{1..10}
[root@localhost~]# find /var/ ! -name 'file9' -exec rm -f {} \;
且或非 选项
| 参数 |
作用 |
| -a (默认) |
且 、和 |
| -o |
或者 |
| ! |
取反 |
多选项条件组合查找
# 示例:
## 按文件类型和目录深度层级查找
[root@localhost~]# find /etc -type d -maxdepth -2
## 按文件名和文件大小组合查找
[root@localhost~]# find /root/dir1 -name'file5'-o -name'file9' -o -size -1M
巩固加深
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]# echo `ls -l `which cp``
total 0 -rw-r--r-- 1 root root 0 Apr 15 00:57 zls.txtwhich cp
[root@localhost opt]# echo $(ls -l $(which cp))
-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的文件 并删除
思维导图