搜索指令


搜索查找指令

find查找文件

从指定目录向下递归,遍历各个子目录,将满足条件的文件或目录显示在终端。所以范围越小找得越快。

find 搜索范围 选项	//必须要选项

选项:
name	按照文件名查找
user	按照用户名查找
size	按照文件大小查找

例:在/home下查找hello.txt

find /home -name hello.txt
结果: /home/hello.txt

find /home hello.txt	//如果不加选项,就失败了

例:在/opt下查找用户root的文件

find /opt -user root

例:查找整个Linux系统下,大于、小于、等于200M的文件

+n	-n  n 单位:k,M,G
find / -size 200M
find / -size +200M
find / -size -200M

locate查找文件

快速定位文件路径,不遍历整个文件系统,查询比较快

原理:利用之前建立的系统中所有文件名称及路径的locate数据库来实现快速定位

使用前提:

每次运行前要用updatedb指令创建locate数据库,然后再执行locate

例:locate定位hello.txt所在的文件目录

updatedb 		 //创建数据库,必须先执行,不然会返回:没有那个文件或目录
locate hello.txt //定位hello.txt

which查找指令

查看某个指令在哪个目录下

which ls
/usr/bin/ls		//结果

grep 和| 内容查找

grep过滤查找,通常与管道符交互,管道符| 表示将前一个命令的结果传递给后面的指令

grep [choice] finding_content source_file

选项
-n	显示匹配行和行号
-i	忽略字母大小写

例:用cat指令查看文件hello.txt并交给grep查找出yes

cat /home/hello.txt | grep "yes"	//自动找出所有yes

带上选项-n 带上了行号

以下两式等效,显示效果略有差别

cat -n /home/hello.txt | grep "yes"
cat /home/hello.txt | grep -n "yes"