find命令 * 可以查找文件和命令,可以模糊查找和精确查找,查找的范围是整个Linux系统
find命令是一条五星级命令,默认到 / 下面查找,因为linux所有文件都在 / 目录下面。
格式 : find 【查找范围】【查找条件】【动作】
【查找范围】 默认根目录,也可以自己指定范围
【查找条件】 -- 选项
常用查找条件(选项)
-name : 按文件名称查找。
-inname: 按文件名称查找,不区分大小写。
-size : 按文件大小查找。
-user: 按文件属主查找。
-type: 按文件类型查找。
-perm : 按文件权限查找。
-mtime: 按文件更改时间查找,以24小时为单位。
-mmin : 按文件更改时间查找,以分钟为单位。
-maxdepth:按最大深度查找。
-newer: 按比某个文件更新的查找。
-name按名字查找
-name默认是精确查找
根据名字,查找根目录下面的hello文件。
## find 是命令
/是查找的位置
-name 是查找的条件
hello是查找的具体的名字
[root@localhost lianxi]# find / -name hello
/usr/bin/hello
/lianxi/hello
/lianxi/aa/bb/cc/dd/hello
/lianxi/aa/bb/cc/hello
/lianxi/aa/bb/hello
/lianxi/aa/hello
/wuxia/hello
/hello
/sanchuang/hello
如果想要模糊查找,可以与通配符搭配使用。
例如查找出所有以hello开头的文件,在hello后面加一个*号。
*代表任意个字符 --》个或者n个
使用通配符必须用引号将hello引起来,不然linux会将它当成一个路径。
这样就能把以hello开头的文件都找出来,
[root@localhost lianxi]# find / -name "hello*"
/boot/grub2/i386-pc/hello.mod
/usr/bin/hello
/usr/lib/grub/i386-pc/hello.mod
/usr/lib/golang/doc/play/hello.go
/lianxi/hello
/lianxi/hello.c
/lianxi/hello.txt
/lianxi/aa/bb/cc/dd/hello
/lianxi/aa/bb/cc/hello
/lianxi/aa/bb/hello
/lianxi/aa/hello
/wuxia/hello
/jindafu/boot/grub2/i386-pc/hello.mod
/hello
/sanchuang/hello.c
/sanchuang/hello
/go/hello.go
如果想要查找出名字里面包含hello的,就用通配符*将hello包含起来
[root@localhost lianxi]# find / -name "*hello*"
/boot/grub2/i386-pc/hello.mod
/usr/bin/hello
/usr/lib/grub/i386-pc/hello.mod
/usr/lib/golang/doc/play/hello.go
/usr/lib64/python2.7/__phello__.foo.py
/usr/lib64/python2.7/__phello__.foo.pyc
/usr/lib64/python2.7/__phello__.foo.pyo
/usr/lib64/python3.6/__phello__.foo.py
/usr/lib64/python3.6/__pycache__/__phello__.foo.cpython-36.opt-1.pyc
/usr/lib64/python3.6/__pycache__/__phello__.foo.cpython-36.opt-2.pyc
/usr/lib64/python3.6/__pycache__/__phello__.foo.cpython-36.pyc
/lianxi/hello
/lianxi/hello.c
/lianxi/hello.txt
/lianxi/aa/bb/cc/dd/hello
/lianxi/aa/bb/cc/hello
/lianxi/aa/bb/hello
/lianxi/aa/hello
/wuxia/hello
/jindafu/boot/grub2/i386-pc/hello.mod
/hello
/sanchuang/hello.c
/sanchuang/hello
/go/hello.go
-iname 按名字查找,但是不区分大小写
用-iname时候它会将所有名字等于xiaomi的文件都找出来,但是不区分大小写。
[root@localhost lianxi]# find / -iname xiaomi
/lianxi/XIAOMI
/lianxi/xiaomi
[root@localhost lianxi]#
-size 按文件大小来查找 黑洞文件 /dev/null
+1k 大于kb的文件 k要小写
-1M 小于1Mb的文件
+1G 大于1G的文件
[root@localhost lianxi]# find . -size +1k
.
./.for.sh.swp
./sc_game_v2.sh
./passwd
[root@localhost lianxi]# find . -size -1M
./chouguo.txt
./hello
./hello.c
./qwer.txt
./DAMI
./XIAOMI
./hello.txt
./dami
./luckylist.txt
./aa/bb/cc/dd/hello
./aa/bb/cc/hello
./aa/bb/hello
./aa/hello
./xiaomi
[root@localhost lianxi]#
2> /dev/null 意思是将错误的信息就丢到黑洞文件里面去。
2>代表错误的输出重定向
/dev 是存放设备文件的目录 device
Linux 里每一个硬件会有一个文件和它对应,例如:鼠标,键盘,网卡,显示器,磁盘等设备
Linux里面一切皆文件
null 是一个Linux 里的黑洞文件,任何的内容放到这个文件里都会消失,相当于一个垃圾处理站
有些东西我们不想保存到磁盘里,也不想看到,所以我们定向到/dev/null
例如: 不加黑洞文件之前
因为在/目录下面没有超过100M的文件,所以会产生报错信息。
[root@localhost lianxi]# find / -size +100M
/proc/kcore
find: ‘/proc/2719/task/2719/fd/6’: 没有那个文件或目录
find: ‘/proc/2719/task/2719/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/2719/fd/5’: 没有那个文件或目录
find: ‘/proc/2719/fdinfo/5’: 没有那个文件或目录
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/usr/lib/locale/locale-archive
现在加入黑洞文件,错误信息就全部都丢入黑洞文件 /dev/null里面了。
[root@localhost lianxi]# find / -size +100M 2>/dev/null
/proc/kcore
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/usr/lib/locale/locale-archive
[root@localhost lianxi]#
-user 根据属主来查找
因为是在当前目录下面查找的,所以只有这么多。
[root@localhost lianxi]# find . -user root
.
./.li
./.adwd
./bigfile.py
./prefix_name.sh
./hosts
./bbigfile.py
./luckygame.sh
./.for.sh.swp
./name.txt
./chouguo.txt
./unluckygame.sh
./file.txt
./hello
./hello.c
./qwer.txt
./DAMI
./XIAOMI
./1.txt
./luckyname.txt
./hello.txt
./python
./python/python-test
./python/python-test/python.py
./backup
./dami
./rm_file.sh
./luckylist.txt
./sc_game_v2.sh
./sc_game.sh
./aa
./aa/bb
./aa/bb/cc
./aa/bb/cc/dd
./aa/bb/cc/dd/hello
./aa/bb/cc/hello
./aa/bb/hello
./aa/hello
./xiaomi
./passwd
-type 根据文件类型来查找
校招题目:
说说有哪几种文件类型
f 类型为文件 file
d 类型为文件夹(目录) directory
l 类型为链接文件 link
b 类型为块设备文件 block
c 类型为字符设备文件 character
p 类型为管道文件 pipe
s 类型为socket文件
[root@localhost lianxi]# find . -type f
./bigfile.py
./prefix_name.sh
./hosts
./bbigfile.py
./luckygame.sh
./.for.sh.swp
./name.txt
./chouguo.txt
./unluckygame.sh
./file.txt
./hello
./hello.c
./qwer.txt
./DAMI
./XIAOMI
./1.txt
./luckyname.txt
./hello.txt
./python/python-test/python.py
./dami
./rm_file.sh
./luckylist.txt
./sc_game_v2.sh
./sc_game.sh
./aa/bb/cc/dd/hello
./aa/bb/cc/hello
./aa/bb/hello
./aa/hello
./xiaomi
./passwd
[root@localhost lianxi]# find . -type d
.
./.li
./.adwd
./python
./python/python-test
./backup
./aa
./aa/bb
./aa/bb/cc
./aa/bb/cc/dd
-mtime 根据更改时间来查找
-mtime以24小时为单位
例如:查找7天前修改过的文件:
find . -mtime +7 -type f
查找7天以内修改过的文件:
find . -mtime -7 -type f
-mmin 根据更改时间来查找
-mmin以分钟为单位。
查找60分钟以内修改的文件
[root@localhost lianxi]# find . -mmin -60
.
./qwer.txt
查找60分钟以前修改的文件
[root@localhost lianxi]# find . -mmin +60
-maxdepth 按照最大深度查找
-maxdepth 查找文件的时候,目录的深度
1.代表当前
2.代表下一级目录
3.下一级的下一级目录,以此类推
[root@localhost lianxi]# mkdir aa/bb/cc/dd -p
[root@localhost lianxi]# cp hello aa
[root@localhost lianxi]# cp hello aa/bb
[root@localhost lianxi]# cp hello aa/bb/cc
[root@localhost lianxi]# cp hello aa/bb/cc/dd
[root@localhost lianxi]# tree aa
aa
├── bb
│ ├── cc
│ │ ├── dd
│ │ │ └── hello
│ │ └── hello
│ └── hello
└── hello
[root@localhost lianxi]# find . -maxdepth 1 -name hello
./hello
[root@localhost lianxi]# find . -maxdepth 2 -name hello
./hello
./aa/hello
[root@localhost lianxi]# find . -maxdepth 3 -name hello
./hello
./aa/bb/hello
./aa/hello
[root@localhost lianxi]# find . -maxdepth 4 -name hello
./hello
./aa/bb/cc/hello
./aa/bb/hello
./aa/hello
【动作】
常用动作:
-exec :将find查找的结果交给-exec 后面的命令执行
-ok
例如: 将hello文件找出来并且复制到/back 目录下
find / -name hello.c -exec cp {} /back \;
-exec 需要执行后面的命令, execute
cp {} /backup 执行cp命令
{ } 代表前面find找到的内容,相当于一个容器,存放前面find找到的内容
/backup 是cp到的目的地·
\; 是find命令的结束符
[root@localhost lianxi]# find /lianxi -name 11.txt -ok rm -rf {} \;
< rm ... /lianxi/11.txt > ? y
[root@localhost lianxi]#
命令执行后会让确认一次
多条件组合
-a (and): 逻辑与,系统默认是与,可不加,表示只有当所给的条件都满足时,寻找条件才算满足。
- o (or): 逻辑或,只要所给的条件中有一个满足,寻找条件就算满足
-not : 逻辑非,在命令中可用 “!”表示,该运算符表示查找不满足所给条件的文件
-a 的优先级默认高,先执行逻辑与,再执行逻辑或
例如:
什么都不接时候,默认是 and
find . -user root -type f -size +1M -o -name "vmlinuz"
这里分成了两部分,-o前面和后面。前面三个选项默认用and连接起来。-o后面是一部分
在find里面如果想用()的话要用\来转义,不然会报错
find . -user root -type f \(-size +1M -o -name "vmlinuz*" \)
组合运用
[root@localhost lianxi]# find / -type f -name "hello*"
/boot/grub2/i386-pc/hello.mod
/usr/bin/hello
/usr/lib/grub/i386-pc/hello.mod
/usr/lib/golang/doc/play/hello.go
/lianxi/hello
/lianxi/hello.c
/lianxi/hello.txt
[root@localhost lianxi]# find / -type f -name "hello*" -size +1k
/boot/grub2/i386-pc/hello.mod
/usr/bin/hello
/usr/lib/grub/i386-pc/hello.mod
/jindafu/boot/grub2/i386-pc/hello.mod
/sanchuang/hello
[root@localhost lianxi]#
[root@localhost lianxi]# find / -type f -name "hello*" -size +1M