1 find 指令
find 指令将从指定目录向下递归的遍历其各个子目录,将满足条件的文件或者目录显示在终端。
基本语法:
find [搜索范围] [选项] (搜索范围主要是指定在哪一个目录下搜索)
选项说明:
选项 | 说明 |
-name <查询方式> | 按照指定的文件名查找模式查找文件 |
-user <用户名> | 查找属于指定用户名的所有文件 |
-size <文件大小> | 按照指定的文件大小查找文件 |
应用案例:
1)按文件名:根据名称查找 /home 目录下的 hello.txt 文件
find /home -name hello.txt
[root@wangweicentos ~]# cd
[root@wangweicentos ~]# find /home -name mydate.txt
/home/wangwei/mydate.txt
/home/mydate.txt
2)按拥有者:查找 /opt 目录下,用户名为 root 的文件
find /opt -user root
[root@wangweicentos ~]# find /opt -user root
/opt
/opt/rh
3)查找整个 linux 系统下大于 200M 的文件
find /size +200M
[root@wangweicentos ~]# find / -size +200M
/proc/kcore
find: ‘/proc/7051/task/7051/fd/5’: No such file or directory
find: ‘/proc/7051/task/7051/fdinfo/5’: No such file or directory
find: ‘/proc/7051/fd/6’: No such file or directory
find: ‘/proc/7051/fdinfo/6’: No such file or directory
find: ‘/run/user/1000/gvfs’: Permission denied
/run/media/wangwei/CentOS 7 x86_64/LiveOS/squashfs.img
[root@wangweicentos ~]# cd /run/media/wangwei/CentOS\ 7\ x86_64/LiveOS/
[root@wangweicentos LiveOS]# ls
squashfs.img TRANS.TBL
[root@wangweicentos LiveOS]# ls -lh
total 498M
-rw-r--r--. 3 wangwei wangwei 498M Oct 27 2020 squashfs.img
-r--r--r--. 1 wangwei wangwei 224 Nov 4 2020 TRANS.TBL
2 locate 指令
locate 指令可以快速定位文件路径,locate指令利用事先建立的系统中所有的文件名称及路径的locate 数据库实现快速定位给定的文件。locate 指令无需遍历整个文件系统,查询速度较快。为了保证查询结果的准确度,管理员必须定期更新 locate 时刻。
基本语法:
locate 搜索文件
特别说明:由于locate 指令基于数据库进行查询,所以第一次运行前,必须使用 updatedb 指令创建 locate 数据库。(每次关机重启后,都需重新创建)
[root@wangweicentos ~]# updatedb
[root@wangweicentos ~]# locate mydate.txt
/home/mydate.txt
/home/wangwei/mydate.txt
3 which 指令
可以查看某个指令在哪个目录下
比如:ls 指令在哪个目录下
[root@wangweicentos ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@wangweicentos ~]# which reboot
/usr/sbin/reboot
4 grep 指令和管道符号 |
grep 过滤查找,管道符 "|" 表示将前一个命令的处理结果输出传递给后面的命令处理。(将 "|" 前面的结果当作 "|" 后面的开始)
基本语法:grep [选项] 查找内容源文件
常用选项:
选项 | 功能 |
-n | 显示匹配的行及行号 |
-i | 忽略字母大小 |
举例:请在 mydate.txt 文件中,查找"yes"所在行,并且显示行号
[root@wangweicentos ~]# cat /home/mydate.txt | grep -n "yes"
39:yes
43:yes
[root@wangweicentos ~]# grep -ni "yes" /home/mydate.txt
39:yes
41:Yes
43:yes