1.find命令 --用来查找文件或文件夹
(1) -name 根据文件名称查找
-iname 根据文件名称查找,但不区分大小写 i:ignore
[root@huyuxuan lianxi]# mkdir hyx HYX
[root@huyuxuan lianxi]# find / -name hyx
/lianxi/hyx
[root@huyuxuan lianxi]# find / -iname hyx
/lianxi/HYX
/lianxi/hyx
同时也可以使用通配符:(比如查找/lianxi文件夹下所有带.txt后缀的文件)
[root@huyuxuan lianxi]# find /lianxi -name "*.txt"
(2) -size 根据文件大小查找
+1k 文件大小大于1k
-1k 文件大小小于1k
1k 文件大小1k左右 (1M,1G,1T也是一样的)
比如查找/boot文件夹里大于10M小于20M的文件:
[root@huyuxuan lianxi]# find /boot -size +10M -size -20M
/boot/initramfs-4.18.0-193.el8.x86_64kdump.img
[root@huyuxuan lianxi]# ls -lh /boot/initramfs-4.18.0-193.el8.x86_64kdump.img
-rw-------. 1 root root 19M 5月 6 15:57 /boot/initramfs-4.18.0-193.el8.x86_64kdump.img
(3) -type 根据文件类型查找
d:文件夹 directory
f:普通文件 file
l:链接文件 link
s:socket文件 --进程和进程之间通信使用的
p:pipe文件,管道文件 --进程和进程之间通信使用的
c:字符设备文件 character
b:块设备文件 block
举例查找一下文件夹和文件:
[root@huyuxuan xiaohu]# mkdir abc.py
[root@huyuxuan xiaohu]# touch aaa.py bbb.py ccc.py
[root@huyuxuan xiaohu]# find /xiaohu -name "*.py" -type d (查找以.py后缀结尾的文件夹)
/xiaohu/abc.py
[root@huyuxuan xiaohu]# find /xiaohu -name "*.py" -type f (查找以.py后缀结尾的文件)
/xiaohu/aaa.py
/xiaohu/bbb.py
/xiaohu/ccc.py
(4) -mtime 根据文件更改时间查找(以24小时为单位)
-mtime 4 表示4天前那一天
-mtime -4 表示4天内
-mtime +4 表示4天前
举例:
查找/lianxi文件夹下7天之前的文件:
[root@huyuxuan /]# find /lianxi -mtime +7
查找/lianxi文件夹下7天内的文件:
[root@huyuxuan /]# find /lianxi -mtime -7
(5) -mmin 根据文件更改时间查找(以分钟为单位)
-mmin -60 表示一个小时内
-mmin +60 表示一个小时前
查找/lianxi文件夹下一个小时之前的文件:
[root@huyuxuan lianxi]# find /lianxi -mmin +60
查找/lianxi文件夹下一个小时内的文件:
[root@huyuxuan lianxi]# find /lianxi -mmin -60
(6) -exec 将find查找的结果交给-exec后面的命令执行
举例:将xiaohu文件夹内所有带.txt后缀的文件删除:
[root@huyuxuan xiaohu]# ls
aa.txt bb.txt cc.txt suda
[root@huyuxuan xiaohu]# find . -name "*.txt" -exec rm -rf {} \;
[root@huyuxuan xiaohu]# ls
suda
{} 表示前面的find查找到的内容
\; 表示find命令的结束符号
举例:找出/xiaohu文件夹下7天内的文件并删除:
[root@huyuxuan xiaohu]# ls
aa.txt bb.txt cc.txt suda
[root@huyuxuan xiaohu]# find /xiaohu -mtime -7 -type f -exec rm -rf {} \;
[root@huyuxuan xiaohu]# ls
suda
(7) -ok 和-exec用法类似,只是比-exec动作多了一个确认
[root@huyuxuan xiaohu]# find /tmp -name "*.py" -type f -ok rm -rf {} \;
< rm ... /tmp/hyx.py > ? y
< rm ... /tmp/suda.py > ? y
(8) -maxdepth 表示查找文件时目录的深度
1代表当前
2代表下一级目录
3下一级的下一级目录,以此类推
[root@huyuxuan xiaohu]# find /xiaohu -maxdepth 1 -name "*.txt" -type d
/xiaohu/hunan.txt
[root@huyuxuan xiaohu]# find /xiaohu -maxdepth 2 -name "*.txt" -type d
/xiaohu/hunan.txt
/xiaohu/hunan.txt/yueyang.txt
[root@huyuxuan xiaohu]# find /xiaohu -maxdepth 3 -name "*.txt" -type d
/xiaohu/hunan.txt
/xiaohu/hunan.txt/yueyang.txt
/xiaohu/hunan.txt/yueyang.txt/junshan.txt
(9) -user 指定用户
(10)特殊查找条件
-a:逻辑与,系统默认是与,-a可不加,表示两边的条件都要满足 and
-o:逻辑或,只需要满足所给条件中的一个就可以 or
-not:逻辑非,在命令中可用"!"表示,该运算符表示查找不满足所给条件的文件
() 用来改变优先级的
举例:
查找/xiaohu文件夹下以.py后缀结尾且大小小于1k的文件或是以.txt后缀结尾且大小小于1k的文件:
[root@huyuxuan xiaohu]# ls
changsha.txt suda suda.py yueyang.txt
[root@huyuxuan xiaohu]# find /xiaohu -name "*.py" -size -1k -o -name "*.txt" -size -1k
/xiaohu/suda.py
/xiaohu/changsha.txt
/xiaohu/yueyang.txt
查找/xiaohu文件夹下大小小于1k的文件或以.txt后缀结尾的文件:
[root@huyuxuan xiaohu]# find . -type f \( -size -1k -o -name "*.txt" \)
./suda.py
./changsha.txt
./yueyang.txt
2. which命令 :查询Linux命令存放的绝对路径
比如查找mkdir命令的绝对路径:
[root@huyuxuan /]# which mkdir
/usr/bin/mkdir
3. whereis命令 :查找文件的路径,原理和which类似
which命令和whereis命令的异同:
相同点:都是在PATH变量里查找
不同点:
1)which查找的命令必须要有可执行权限,才能找到;而whereis不需要可执行权限
2)whereis查找命令时会多显示该命令的man手册路径
[root@huyuxuan /]# which mkdir
/usr/bin/mkdir
[root@huyuxuan /]# whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz
4. locate命令 :用来查找指定文件或文件夹
locate命令查找的东西有滞后性,linux系统默认updatedb命令一天自动执行一次
如果是要查找刚刚新建的文件的话,直接用locate命令是无法查询到的,得先使用updatedb命令更新一下后台数据库,之后就能够查到了
[root@huyuxuan lianxi]# touch suda.txt
[root@huyuxuan lianxi]# locate suda.txt
[root@huyuxuan lianxi]# updatedb (更新数据库,生成locate需要的数据库文件mlocate.db)
[root@huyuxuan lianxi]# locate suda.txt
/lianxi/suda.txt