linux下查找命令(find、grep)总结

前言

find和grep是linux下常用的两个查找命令,find命令侧重于查找目录下的文件,grep命令侧重于查找文件中的内容。find查找可以指定文件名、文件类型、文件大小、文件权限、文件的修改时间等,grep还可以通过管道和ls命令、ps命令、编译工具等结合起来,以便得到更加准确的信息。

一、grep命令

1.1、查找文件中指定字符串

  • grep “字符串” 文件名
grep "hello" test.txt //查找文件名为test.txt的文件中包含hello字符串的行

输出结果:

ubuntu@ubuntu:~/Desktop$ grep "hello" test.txt
hello world!
hello world!
ubuntu@ubuntu:~/Desktop$ 

```c
- grep -i -n "字符串" 文件名

grep -i -n “hello” test.txt 查找文件名为test.txt的文件中包含hello字符串的行(不区分大小写,并显示行号)

输出结果:

```c
ubuntu@ubuntu:~/Desktop$ grep -i -n "hello" test.txt
1:hello world!
2:hello world!
3:Hello world!
4:HELLO world!
ubuntu@ubuntu:~/Desktop$ 
  • grep -e “字符串1” -e “字符串2” 文件名
grep -e "hello" -e "china" test.txt //查找名为test.txt的文件中包含“hello”或者“china的字符串”

输出结果:

ubuntu@ubuntu:~/Desktop$ grep -e "hello" -e "china" test.txt
hello world!
hello world!
I am from china.
ubuntu@ubuntu:~/Desktop$ 
  • grep -A1 “字符串” 文件名
grep -A1 "hello" test.txt //查找文件名为test.txt中包含“hell”字符串的行,并显示最后一个匹配结果下一行的内容

输出结果:

ubuntu@ubuntu:~/Desktop$ grep -A1 "HELLO" test.txt
HELLO world!
I am from china.
ubuntu@ubuntu:~/Desktop$ 

  • grep “^字符串” 文件名
grep "^he" test.txt //查找test.txt文件中以“he”开头的行

输出结果:

ubuntu@ubuntu:~/Desktop$ grep "^he" test.txt
hello world!
hello world!
ubuntu@ubuntu:~/Desktop$ 

  • grep “字符串” 文件1 文件2 … 文件n
grep "hello" test1.txt test2.txt test3.txt //查找三个文件中匹配字符串“hello”的行
  • 其它常用组合
# 搜索当前目录下递归搜索
grep "text" . -r -n

  • grep常用的选项
选项 说明
-i 忽略大小写
-n 显示结果所在行号
-c 统计符合条件的总行数并输出行数
-v 反向匹配,输出不带关键词的行
-e 实现多个选项的匹配
-Ax 显示匹配结果最后一行之后的x行
-Bx 显示匹配结果第一行之前的x行
-o 只显示符合条件的字符串,但是不显示整行
^ 匹配指定字符串开头的行
$ 匹配指定字符串结尾的行
-r 在目录中搜索匹配字符串的行
-E 使用正则表达式,相当于egrep

1.2、grep通过管道和其它命令组合使用

  • 列出当前目录下后缀名为“.txt”的文件
ls | grep *.txt 
  • 显示进程中包含“2221”字符串的进程
ps aux | grep 2221

二、find命令

2.1、按文件名和i节点搜索

  • find命令格式
find 搜索路径 [选项] 搜索内容
  • find [路径]
find       #列出当前目录下所有文件
find Desktop/ #列出Desktop目录下的所有文件 
  • find [路径] -name [文件名]
find ./ -name libc.a #查找当前目录下名为libc.a的文件
  • find [路径] -iname [文件名]
find ./ -iname libc.a #查找当前目录下名为libc.a的文件,不区分大小写
  • find [路径] -inum [i节点编号]
find ./ -inum 393242 #查找当前目录下i节点编号为393242的文件

2.2、按文件大小搜索

  • find [路径] -size [+/-][大小]
find ./ -size +5024k #查找当前目录下文件大于5024KB的文件
find ./ -size -1k #查找当前目录下小于1KB的文件
find ./ -size +1M #查找当前目录下大于1M的文件
find ./ -size +1G #查找当前目录下大于1GB的文件
find ./ -size +1c #查找当前目录下大于1byte的文件
find ./ -size +1c #查找当前目录下大于1个block(512bytes)的文件
find ./ -size 1k #查找当前目录下文件大小等于1kB的文件

2.3、按文件访问和修改时间搜索

  • find [路径] -atime [+/-][天数]
find ./ -atime -1 #列出一天内访问过的文件
find ./ -atime 0 #列出当天访问过的文件
find ./ -atime +1 #列出一天前访问过的文件
  • find [路径] -mtime [+/-][天数]
find ./ -atime -1 #列出一天内修改过的文件
find ./ -atime 0 #列出当天修改过的文件
find ./ -atime +1 #列出一天前修改过的文件

2.4、按文件类型搜索

  • find [路径] -type [查找类型]
find ./ -type d #查找当前目录下的所有目录文件
find ./ -type f #查找当前目录下的所有普通文件
find ./ -type l #查找当前目录下的所有链接文件

2.5 组合搜索

  • 使用与、或、非逻辑
-a:and逻辑与
-o:or逻辑或
-not:not 逻辑非
find ./ -size +1k -a -type d #查找当前目录下大于1KB并且类型为目录的文件

2.6 使用通配符搜索

  • [*]表示匹配任何字符
  • [?]表示匹配单个字符
find ./ -name "*.txt" #查找当前目录下所有以“.txt”结尾的文件
find ./ -name "?.txt" #查找当前目录下所有文件名只有一个字符,并且以“.txt”结尾的文件
find ./ -name "????.txt" #查找当前目录下所有文件名只有四个字符,并且以“.txt”结尾的文件

猜你喜欢

转载自blog.csdn.net/weixin_39270987/article/details/127663662