1.find查找文件

1.-name pattern:按文件名查找,支持使用通配符 * 和 ?。
2.-type type:按文件类型查找,可以是 f(普通文件)、d(目录)、l(符号链接)等。
3.-size [+-]size[cwbkMG]:按文件大小查找,支持使用 + 或 - 表示大于或小于指定大小,单位可以是 c(字节)、w(字数)、b(块数)、k(KB)、M(MB)或 G(GB)。
4.-mtime days:按修改时间查找,支持使用 + 或 - 表示在指定天数前或后,days 是一个整数表示天数。
5.-user username:按文件所有者查找。
6.-group groupname:按文件所属组查找。
7.-maxdepth 1按照深度等级查找。

find 命令中用于时间的参数如下:
   -amin n :查找在 n 分钟内被访问过的文件。
   -atime n:查找在 n*24 小时内被访问过的文件。
   -cmin n :查找在 n 分钟内状态发生变化的文件(例如权限)。
   -ctime n:查找在 n*24 小时内状态发生变化的文件(例如权限)。
   -mmin n :查找在 n 分钟内被修改过的文件。
   -mtime n:查找在 n*24 小时内被修改过的文件。
   
-mtime 0 表示查找今天修改过的文件,-mtime -7 表示查找一周以前修改过的文件。
关于时间 n 参数的说明:
   +n:查找比 n 天前更早的文件或目录。
   -n:查找在 n 天内更改过属性的文件或目录。
   n:查找在 n 天前(指定那一天)更改过属性的文件或目录。
#.................................................................   
find命令 查找文件
语法格式:
        find 路径 文件的名称   # 按名字查找
        find 路径 类型  f      # 按类型查找
        find 路径 类型  f 文件名称 # 查找具体类型的名字
        
案例1: 按照名字查找
   格式:find ./  -name "1.txt"
   [root@linuxsd ~]# find ./ -name "1g.txt"
   ./1g.txt        
   
查找/opt目录下的1.log文件
   [root@linuxsd ~]# ll /opt/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 10:50 1.log
   -rw-r--r-- 1 root root 0 Jul 12 10:51 1.txt
   -rw-r--r-- 1 root root 0 Jul 12 10:51 2.txt
   -rw-r--r-- 1 root root 0 Jul 12 10:51 3.txt
   -rw-r--r-- 1 root root 0 Jul 12 10:51 4.txt

   [root@linuxsd ~]# find /opt/ -name "1.log"
   /opt/1.log   
   
案例2: 查找所有.txt结尾的
   [root@linuxsd ~]# find /opt/ -name "*.txt"
   /opt/1.txt
   /opt/2.txt
   /opt/3.txt
   /opt/4.txt   ·
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 10:55 11.txt
   -rw-r--r-- 1 root root 0 Jul 12 10:50 1.log
   -rw-r--r-- 1 root root 0 Jul 12 10:51 1.txt
   -rw-r--r-- 1 root root 0 Jul 12 10:51 2.txt
   -rw-r--r-- 1 root root 0 Jul 12 10:51 3.txt
   -rw-r--r-- 1 root root 0 Jul 12 10:51 4.txt
   [root@linuxsd ~]# find /opt/ -name "[0-9].txt"
   /opt/1.txt
   /opt/2.txt
   /opt/3.txt
   /opt/4.txt
   [root@linuxsd ~]# find /opt/ -name "[0-9][0-9].txt"
   /opt/11.txt
   
案例3: 不区分大小写
   [root@linuxsd ~]# ll /opt/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 10:50 1.log
   -rw-r--r-- 1 root root 0 Jul 12 10:56 1.LOG
   -rw-r--r-- 1 root root 0 Jul 12 10:56 2.LOG
   -rw-r--r-- 1 root root 0 Jul 12 10:56 3.LOG
   
   [root@linuxsd ~]# find /opt/ -iname "*.log"
   /opt/1.log
   /opt/1.LOG
   /opt/2.LOG
   /opt/3.LOG
   
案例4: 按照文件类型查找
   f  普通文件 常用
   d  目录     常用
   l  软链接
   c  字节
   b  块设备
查找/opt目录下所有的普通文件
   [root@linuxsd ~]# mkdir /opt/oldboy{1..4}
   [root@linuxsd ~]# ll /opt/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 10:50 1.log
   -rw-r--r-- 1 root root 0 Jul 12 10:56 1.LOG
   -rw-r--r-- 1 root root 0 Jul 12 10:56 2.LOG
   -rw-r--r-- 1 root root 0 Jul 12 10:56 3.LOG
   drwxr-xr-x 2 root root 6 Jul 12 10:59 oldboy1
   drwxr-xr-x 2 root root 6 Jul 12 10:59 oldboy2
   drwxr-xr-x 2 root root 6 Jul 12 10:59 oldboy3
   drwxr-xr-x 2 root root 6 Jul 12 10:59 oldboy4

   [root@linuxsd ~]# find /opt/ -type f  查找文件
   /opt/1.log
   /opt/1.LOG
   /opt/2.LOG
   /opt/3.LOG
   
案例5.查找所有的目录
   [root@linuxsd ~]# find /opt/ -type d  查找目录
   /opt/
   /opt/oldboy1
   /opt/oldboy2
   /opt/oldboy3
   /opt/oldboy4   

案例6: 组合 查找普通文件名称为2.LOG的文件 #不对,不是查找2.LOG文件的
   [root@linuxsd ~]# touch /opt/5
   [root@linuxsd ~]# ll /opt/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 10:50 1.log
   -rw-r--r-- 1 root root 0 Jul 12 10:56 1.LOG
   -rw-r--r-- 1 root root 0 Jul 12 10:56 2.LOG
   -rw-r--r-- 1 root root 0 Jul 12 10:56 3.LOG
   -rw-r--r-- 1 root root 0 Jul 12 11:01 5
   drwxr-xr-x 2 root root 6 Jul 12 10:59 oldboy1
   drwxr-xr-x 2 root root 6 Jul 12 10:59 oldboy2
   drwxr-xr-x 2 root root 6 Jul 12 10:59 oldboy3
   drwxr-xr-x 2 root root 6 Jul 12 10:59 oldboy4
   [root@linuxsd ~]# find /opt/ -name "5"
   /opt/5
   
-------------------------------------------------------------
   [root@linuxsd ~]# touch /opt/oldboy5
   [root@linuxsd ~]# mkdir /tmp/oldboy5
查找文件为oldboy5
   [root@linuxsd ~]# find / -type f -name "oldboy5"

案例7: 按照inode查找 
   [root@linuxsd ~]# find /opt/ -inum 17340825
   /opt/5

案例8: 按照深度等级查找 -maxdepth 1
解释:#maxdepth是用来确定find查找目录的深度。默认的情况下,find命令会对当前目录及其子目录中的所有文件和目录都进行搜索。但如果我们只想在当前目录下或某个固定深度目录下查找,就可以使用maxdepth参数。

[root@linuxsd ~]# tree /opt/
/opt/
├── 1.log
├── 1.LOG
├── 2.LOG
├── 3.LOG
├── oldboy1
│   ├── 10.txt
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   ├── 5.txt
│   ├── 6.txt
│   ├── 7.txt
│   ├── 8.txt
│   └── 9.txt
├── oldboy2
├── oldboy3
└── oldboy4

   [root@linuxsd ~]# find /opt/ -type f
   /opt/1.log
   /opt/1.LOG
   /opt/2.LOG
   /opt/3.LOG
   /opt/oldboy1/1.txt
   /opt/oldboy1/2.txt
   /opt/oldboy1/3.txt

   [root@linuxsd ~]# find /opt/ -maxdepth 1 -type f
   /opt/1.log
   /opt/1.LOG
   /opt/2.LOG
   /opt/3.LOG

案例9: 按照文件的大小进行查找
   find ./ -size 10M  # 等于10M的文件
   find ./ -size -10M # 小于10M的文件
   find ./ -size +10M # 大于10M的文件

案例1: 查找当前目录大于10M的文件
   [root@linuxsd ~]# find ./ -size +10M
   ./gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
   ./1g.txt
   
如何查看文件的大小: ll -h  #查看文件大小还有:du
   [root@linuxsd ~]# ll -h gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm 
   -rw-r--r-- 1 root root 372M Aug 22  2018 gitlab-ce-10.2.2-  ce.0.el7.x86_64.rpm
   [root@linuxsd ~]# ll -h 1g.txt 
   -rw-r--r-- 1 root root 1000M Jul 12 10:26 1g.txt  
   
案例2: 查找文件小于10M的
   [root@linuxsd ~]# find /opt/ -size -10M
   /opt/
   /opt/1.log
   /opt/1.LOG
   /opt/2.LOG
   /opt/3.LOG
   

案例3: 查找文件等于10M的
   [root@linuxsd ~]# find ./ -size 10M
   [root@linuxsd ~]# dd if=/dev/zero of=10m.txt bs=1M count=10
   10+0 records in
   10+0 records out
   10485760 bytes (10 MB) copied, 0.0591945 s, 177 MB/s
   [root@linuxsd ~]# find ./ -size 10M
   ./10m.txt
   [root@linuxsd ~]# ll -h 10m.txt 
   -rw-r--r-- 1 root root 10M Jul 12 11:28 10m.txt
   
案例4: 查找文件大于300M 小于500M的
   [root@linuxsd ~]# find ./ -size +300M -size -500M
   ./gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
   [root@linuxsd ~]# ll -h gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm 
   -rw-r--r-- 1 root root 372M Aug 22  2018 gitlab-ce-10.2.2-  ce.0.el7.x86_64.rpm
   [root@linuxsd ~]# find ./ -size +300M -size -500M -name  "gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm"
   ./gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
   
默认为并且关系:
   [root@linuxsd ~]# find ./ -type f -size +300M -size -500M
   ./gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm   
   
案例5: 查找文件大于300M的 或者名称为1.txt
   [root@linuxsd ~]# find ./  -type f -size +300M -or -name  "1.txt"
   ./gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
   ./1g.txt
   [root@linuxsd ~]# touch 1.txt
   [root@linuxsd ~]# find ./  -type f -size +300M -or -name "1.txt"
   ./gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
   ./1g.txt
   ./1.txt  
   
企业中经常用来查找系统中的大文件:
   [root@linuxsd ~]# find / -size +500M
   
   
案例10: find按照时间查找
find ./ -mtime 7  第7天的
find ./ -mtime -7 7天内的
find ./ -mtime +7 7天前的

案例. 查找7天内被修改过的文件
环境准备:
[root@linuxsd opt]# rm -rf *
[root@linuxsd opt]# 
[root@linuxsd opt]# ll
total 0
[root@linuxsd opt]# touch {1..3}.txt
[root@linuxsd opt]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 12 11:51 1.txt
-rw-r--r-- 1 root root 0 Jul 12 11:51 2.txt
-rw-r--r-- 1 root root 0 Jul 12 11:51 3.txt
[root@linuxsd opt]# date -s 20081010
Fri Oct 10 00:00:00 CST 2008
[root@linuxsd opt]# touch {5..10}.log
[root@linuxsd opt]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 10 00:00 10.log
-rw-r--r-- 1 root root 0 Jul 12  2023 1.txt
-rw-r--r-- 1 root root 0 Jul 12  2023 2.txt
-rw-r--r-- 1 root root 0 Jul 12  2023 3.txt
-rw-r--r-- 1 root root 0 Oct 10 00:00 5.log
-rw-r--r-- 1 root root 0 Oct 10 00:00 6.log
-rw-r--r-- 1 root root 0 Oct 10 00:00 7.log
-rw-r--r-- 1 root root 0 Oct 10 00:00 8.log
-rw-r--r-- 1 root root 0 Oct 10 00:00 9.log
[root@linuxsd opt]# ntpdate ntp1.aliyun.com
12 Jul 11:52:04 ntpdate[7135]: step time server 120.25.115.20 offset 465565903.267279 sec

--------------------------------------------------------

[root@linuxsd opt]# find ./ -mtime -7
./1.txt
./2.txt
./3.txt

案例: 查找7天前被修改过的文件
[root@linuxsd opt]# find ./ -mtime +7
./
./5.log
./6.log
./7.log
./8.log
./9.log
./10.log

查找7天前修改过的文件
[root@linuxsd opt]# find ./ -type f -mtime +7
./5.log
./6.log
./7.log
./8.log
./9.log
./10.log

企业中: 查找7天前 删除7天前的内容 保留最近7天的数据
       查找n天内的 网站被攻击 查找出哪些文件被篡改 find /code/ -mtime -3
find小结:
2.find ./ -iname "name" # 不区分大小写
3.find ./ -name "*.txt" # 不区分大小写
4.find ./ -inum 234343  # 按inode查找
5.find ./ -maxdepth 1   #按照深度等级查找
6.find ./ -type f       # 按照文件类型查找
                d b c 文件类型
7.find ./ -type f -name "1.txt" # 查找文件名称为1.txt
8.find ./ -size +10    # 大于10的  -10小于10M  10 等于10M的
9.find ./ -type f -name "*.log" -size +50M # 并且关系
10.find ./ -type f -mtime +7    # 查找7天前的文件 -7 7天内的文件
11.find ./ -type f -o -size 100M # 查找出所有普通文件或者文件大小为100M的
   

2.find结果交给其他命令

将查找到的文件交给rm mv cp ls cat命令
案例1. 查找/opt/下的1.txt文件 复制到/tmp目录下
   [root@linuxsd ~]# rm -rf /opt/* /tmp/*
   [root@linuxsd ~]# touch /opt/{1..3}.txt
   [root@linuxsd ~]# ll /opt/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 15:26 1.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:26 2.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:26 3.txt

   [root@linuxsd ~]# find /opt/ -name "1.txt"
   /opt/1.txt
将查找到的文件插入到指定的{}内供 cp调用
   [root@linuxsd ~]# find /opt/ -name "1.txt"|xargs -i cp {} /tmp/
   [root@linuxsd ~]# ll /opt/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 15:26 1.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:26 2.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:26 3.txt
   [root@linuxsd ~]# ll /tmp/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 15:29 1.txt


案例2: 查找/tmp/1.txt 然后删除
   [root@linuxsd ~]# find /tmp/ -name "1.txt"
   /tmp/1.txt
   [root@linuxsd ~]# find /tmp/ -name "1.txt"|xargs rm

拷贝多个文件到/tmp目录
#先查看opt里面有那些文件
   [root@linuxsd ~]# find /opt/ -type f|xargs ls -l 
   -rw-r--r-- 1 root root 0 Jul 12 15:26 /opt/1.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:26 /opt/2.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:26 /opt/3.txt
#将opt下的文件复制到tmp下
   [root@linuxsd ~]# find /opt/ -type f|xargs -i cp {}  /tmp/
   [root@linuxsd ~]# ll /tmp/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 15:34 1.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:34 2.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:34 3.txt
   
案例3: 将查找到的文件交给cat查看内容
   [root@linuxsd ~]# find /opt/ -name "1.txt"|xargs cat
   aaaaaaaaaa
   test
   oldboy
   hehe
   
案例4: 将查找到的文件交给mv处理
   [root@linuxsd ~]# find /opt/ -name "1.txt"
   /opt/1.txt
将opt目录下的1.txt移动到tmp目录下   
   [root@linuxsd ~]# find /opt/ -name "1.txt"|xargs -i  mv {} /tmp
查看opt目录下的1.txt是否存在   
   [root@linuxsd ~]# ll /opt/
   total 0
   -rw-r--r-- 1 root root 0 Jul 12 15:26 2.txt
   -rw-r--r-- 1 root root 0 Jul 12 15:26 3.txt
1.txt已移动到tmp目录下   
   [root@linuxsd ~]# ll /tmp/
   total 4
   -rw-r--r-- 1 root root 28 Jul 12 15:34 1.txt  

第二种方式: 使用exec
   [root@linuxsd ~]# find /opt/ -name "2.txt"
   /opt/2.txt
   [root@linuxsd ~]# find /opt/ -name "2.txt" -exec cp {} /tmp/ \;
   [root@linuxsd ~]# ll /tmp/
   total 4
   -rw-r--r-- 1 root root 28 Jul 12 15:34 1.txt
   -rw-r--r-- 1 root root  0 Jul 12 15:40 2.txt
注意: -exec后面不识别别名
   [root@linuxsd ~]# find /opt/ -name "3.txt" -exec ls -l {} \;
   -rw-r--r-- 1 root root 0 Jul 12 15:26 /opt/3.txt
   
删除:
[root@linuxsd ~]# find /opt/ -name "3.txt" -exec rm {}  \;
   
移动:
[root@linuxsd ~]# ll /tmp/
total 4
-rw-r--r-- 1 root root 28 Jul 12 15:34 1.txt
-rw-r--r-- 1 root root  0 Jul 12 15:40 2.txt
[root@linuxsd ~]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Jul 12 15:26 3.txt
[root@linuxsd ~]# find /tmp/ -name "*.txt"
/tmp/1.txt
/tmp/2.txt
[root@linuxsd ~]# find /tmp/ -name "*.txt" -exec mv {} /opt/ \;
[root@linuxsd ~]# ll /tmp/
total 0
[root@linuxsd ~]# ll /opt/
total 4
-rw-r--r-- 1 root root 28 Jul 12 15:34 1.txt
-rw-r--r-- 1 root root  0 Jul 12 15:40 2.txt
-rw-r--r-- 1 root root  0 Jul 12 15:26 3.txt
   
第三种: 反引号或者$()
查看opt下存在哪些文件
   [root@linuxsd ~]# ll /opt/
   total 4
   -rw-r--r-- 1 root root 28 Jul 12 15:34   1.txt
   -rw-r--r-- 1 root root  0 Jul 12 15:40 2.txt
   -rw-r--r-- 1 root root  0 Jul 12 15:26 3.txt
  [root@linuxsd ~]# ll /tmp/
  total 0

案例: 将结果交给cp命令
[root@linuxsd ~]# cp `find /opt/ -name "1.txt"` /tmp/
[root@linuxsd ~]# ll /tmp/
total 4
-rw-r--r-- 1 root root 28 Jul 12 15:46 1.txt

$()和`` 意义相同的
[root@linuxsd ~]# cp $(find /opt/ -name "1.txt") /tmp/

案例2: 将结果交给rm命令删除
[root@linuxsd ~]# rm -f `find /opt/ -name "2.txt"`
[root@linuxsd ~]# ll /opt/
total 4
-rw-r--r-- 1 root root 28 Jul 12 15:34 1.txt
-rw-r--r-- 1 root root  0 Jul 12 15:26 3.txt

案例3: 将结果交给mv命令
[root@linuxsd ~]# ll /tmp/
total 4
-rw-r--r-- 1 root root 28 Jul 12 15:46 1.txt
[root@linuxsd ~]# ll /opt/
total 4
-rw-r--r-- 1 root root 28 Jul 12 15:34 1.txt
-rw-r--r-- 1 root root  0 Jul 12 15:26 3.txt

[root@linuxsd ~]# mv `find /opt/ -name "3.txt"` /tmp/
[root@linuxsd ~]# ll /tmp/
total 4
-rw-r--r-- 1 root root 28 Jul 12 15:46 1.txt
-rw-r--r-- 1 root root  0 Jul 12 15:26 3.txt


将结果交给grep
[root@linuxsd ~]# find /opt/ -type f|xargs grep 'www.cangjie.com'
/opt/code.txt:www.cangjie.com
/opt/2.txt:www.cangjie.com
/opt/3.txt:www.cangjie.com

小结:
三种语法:
1.使用xargs
find /opt/ -name "1.txt"|xargs rm   # 删除
find /opt/ -name "1.txt"|xargs -i cp {} /tmp # 复制
find /opt/ -name "1.txt"|xargs ls -l
2.使用exec
find /opt/ -name "1.txt" -exec rm {} \; # 删除
find /opt/ -name "1.txt" -exec cp {} /tmp \; 复制
find /opt/ -name "1.txt" -exec ls -l {} \; 查看详细信息
3.使用反引号或者$()
rm -rf `find /opt/ -name "1.txt"`
cp `find /opt/ -name "1.txt"` /tmp
ll `find /opt/ -name "1.txt"`
3.特殊符号的作用
; 命令的分隔符 不管前面的命令成功或者失败 都会继续执行
&& 前面命令必须执行成功 才会往后执行
|| 前面命令必须执行失败 才会往后执行


1. 分号 ; 命令的分隔  不管前面的命令执行成功或失败 都会继续往后执行
[root@linuxsd opt]# mkdir 1;echo oldboy;touch test.txt;cd 1
oldboy
[root@linuxsd 1]# ll
total 0
[root@linuxsd 1]# ll ../
total 0
drwxr-xr-x 2 root root 6 Jul 12 16:21 1
-rw-r--r-- 1 root root 0 Jul 12 16:21 test.txt

[root@linuxsd opt]# mkdi 1;ech oldboy;touch test.txt
-bash: mkdi: command not found
-bash: ech: command not found
[root@linuxsd opt]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 12 16:22 test.txt

2. && 命令的分隔符 只有前面的命令执行成功 才会继续往后执行
[root@linuxsd opt]# mkdir 1 && cd 1
[root@linuxsd 1]# 

[root@linuxsd opt]# mkdi 1 && touch test.txt
-bash: mkdi: command not found
[root@linuxsd opt]# ll
total 0

3.|| 命令的分隔符  只有前面的命令执行失败 才会继续往后执行
[root@linuxsd opt]# mkdir 1 || cd 1
[root@linuxsd opt]# ll
total 0
drwxr-xr-x 2 root root 6 Jul 12 16:26 1
[root@linuxsd opt]#  结果没有切换到1目录

[root@linuxsd opt]# ll
total 0
cd失败 执行创建动作
[root@linuxsd opt]# cd 1 || mkdir 1
-bash: cd: 1: No such file or directory
[root@linuxsd opt]# ll
total 0
drwxr-xr-x 2 root root 6 Jul 12 16:26 1


[root@linuxsd ~]# ping -c1 -W1 www.baidu.com &>/dev/null && echo ok
ok
[root@linuxsd ~]# ping -c1 -W1 www.baiduaaaaa.com &>/dev/null && echo ok
[root@linuxsd ~]# ping -c1 -W1 www.baiduaaaaa.com &>/dev/null && echo ok || echo errorerror
[root@linuxsd ~]# ping -c1 -W1 www.baiduaaaaa.com &>/dev/null || echo error
error
[root@linuxsd ~]# ping -c1 -W1 www.baiduaaaaa.com &>/dev/null || echo error && echo okerror
ok
[root@linuxsd ~]# ping -c1 -W1 www.baiduaaaaa.com &>/dev/null || echo error || echo ok 
error

持续分享运维干货,感谢大家的阅读和关注!

请输入图片描述

发表评论

召唤看板娘