搜索查找命令
1. find 查找文件或者目录
find 指令将从指定目录向下递归地遍历其各个子目录,将满足条件的文件显示在终端。
基本语法
find [搜索范围] [选项]
选项说明
选项 | 功能 |
---|---|
-name<查询方式> | 按照指定的文件名查找模式查找文件 |
-user<用户名> | 查找属于指定用户名所有文件 |
-size<文件大小> | 按照指定的文件大小查找文件,单位为:b-块(512 字节)c-字节w-字(2字节)k-千字节M-兆字节G-吉字节 |
sh
## 根据名称查找
[root@hadoop100 ~]# find xiyou/ -name "*.txt"
## 按拥有者查找
[root@hadoop100 ~]# find xiyou/ -user jack
## 按文件大小查找 在/home目录下查找大于200m的文件(+n 大于 -n小于 n等于)
[root@hadoop100 ~]# find /home -size +204800
2. locate 快速定位文件路径
locate 指令利用事先建立的系统中所有文件名称及路径的 locate 数据库实现快速定位给定的文件。Locate 指令无需遍历整个文件系统,查询速度较快。为了保证查询结果的准确度,管理员必须定期更新 locate 时刻
基本语法 locate 搜索文件
警告
由于 locate 指令基于数据库进行查询,所以第一次运行前,必须使用updatedb
指令创建locate
数据库
sh
## 查询文件夹
[root@hadoop100 ~]# updatedb
[root@hadoop100 ~]# locate tmp
3. grep 过滤查找及"|"管道符
管道符,"|",表示将前一个命令的处理结果输出传递给后面的命令处理
基本语法
grep 选项 查找内容 源文件 其中选项:-n 显示匹配行及行号。
sh
## 查询文件夹
[root@hadoop100 ~]# ls | grep -n test
[root@hadoop100 tmp]# grep -n i ks-script-k7wEXv
2:restorecon -ir /etc/sysconfig/network-scripts /var/lib /etc/lvm \
3: /dev /etc/iscsi /var/lib/iscsi /root /var/lock /var/log \
4: /etc/modprobe.d /etc/sysconfig /var/cache/yum /etc/yum
6:# Also relabel the OSTree variants of the normal mounts (if they exist)
7:restorecon -ir /var/roothome /var/home /var/opt /var/srv /var/media /var/mnt
9:restorecon -i /etc/rpm/macros /etc/dasd.conf /etc/zfcp.conf /lib64 /usr/lib64 \
10: /etc/blkid.tab* /etc/mtab /etc/fstab /etc/resolv.conf \
12: /etc/mdadm.conf /etc/sysconfig/network /root/install.log* \
13: /etc/*shadow* /etc/dhcp/dhclient-*.conf /etc/localtime \
14: /etc/hostname /root/install.log*
16:if [ -e /etc/zipl.conf ]; then
17: restorecon -i /etc/zipl.conf
18:fi