Shell | 文件或关键字查询

针对某种文件类型或某个关键字进行全局或局部检索
Updated: 2022 / 5 / 29



文件

find

使用 find 命令 1

  • 应用场景
    假设当前路径下有许多不同类型的文件(e.g. xls, txt, doc),针对某种文件类型、文件创建/修改/访问时间、文件大小进行全局检索

文件类型

区分大小写

对当前路径下某种文件类型 (区分大小写) 进行全局检索
find /home/Desktop -name ‘*.txt’

[root@lnx149]# ls -lh /home/Desktop/
total 7.3M
-rw-rw-r--. 1  		    7.5K Oct 11 17:56 example.txt
drwxrwxr-x. 2     		   6 Oct 12 09:06 new folder
-rwxrwxrwx. 1 root root 7.3M Oct 11 16:36 example1.txt

[root@lnx149]# find /home/Desktop -name '*.txt'
/home/Desktop/example1.txt
/home/Desktop/example.txt

不区分大小写

对当前路径下某种文件类型 (不区分大小写) 进行全局检索
find /home/Desktop -iname ‘*.txt’

[root@lnx149]# ls -lh /home/Desktop/
total 7.3M
-rwxrwxrwx. 1 root root  345 Oct 12 09:09 example1.txt
-rw-rw-r--. 1 		    7.5K Oct 11 17:56 example.txt
-rwxrwxrwx. 1 root root    0 Oct 12 09:21 mess1.txT
-rwxrwxrwx. 1 root root    0 Oct 12 09:21 mess1.Txt
-rwxrwxrwx. 1 root root    0 Oct 12 09:21 mess1.TXT
drwxrwxr-x. 2 		       6 Oct 12 09:06 new folder
-rwxrwxrwx. 1 root root 7.3M Oct 11 16:36 test.txt

[root@lnx149]# find /home/Desktop -iname '*.txt'
/home/Desktop/test.txt
/home/Desktop/example.txt
/home/Desktop/example1.txt
/home/Desktop/mess1.Txt
/home/Desktop/mess1.txT
/home/Desktop/mess1.TXT

时间

访问时间

对当前路径下最近10分钟访问过的文件
find /home/Desktop -amin -10

[root@lnx149]# ls -lh /home/Desktop/
total 7.3M
-rwxrwxrwx. 1 root root  345 Oct 12 09:09 example1.txt
-rwxrwxrwx. 1 root root  693 Oct 12 09:23 example2.txt
-rw-rw-r--. 1 	  	    7.5K Oct 11 17:56 example.txt
drwxrwxr-x. 2 		       6 Oct 12 09:06 new folder
-rwxrwxrwx. 1 root root 7.3M Oct 11 16:36 test.txt

[root@lnx149]# find /home/Desktop -amin -10
/home/Desktop
/home/Desktop/example2.txt

对当前路径下最近48h访问过的文件
find /home/Desktop -atime -2

[root@lnx149]# ls -lh /home/Desktop/
total 7.3M
-rwxrwxrwx. 1 root root  345 Oct 12 09:09 example1.txt
-rwxrwxrwx. 1 root root  693 Oct 12 09:23 example2.txt
-rwxrwxrwx. 1 root root  434 Oct 12 09:28 example3.txt
-rw-rw-r--. 1		    7.5K Oct 11 17:56 example.txt
drwxrwxr-x. 2 		       6 Oct 12 09:06 new folder
-rwxrwxrwx. 1 root root 7.3M Oct 11 16:36 test.txt

[root@lnx149]# find /home/Desktop -atime -2
/home/Desktop
/home/Desktop/test.txt
/home/Desktop/example.txt
/home/Desktop/new folder
/home/Desktop/example1.txt
/home/Desktop/example2.txt
/home/Desktop/example3.txt

修改时间

对当前路径下最近10分钟修改过的文件
find /home/Desktop -mmin -10

[root@lnx149]# ls -lh /home/Desktop/
total 7.3M
-rwxrwxrwx. 1 root root  345 Oct 12 09:09 example1.txt
-rwxrwxrwx. 1 root root  693 Oct 12 09:23 example2.txt
-rwxrwxrwx. 1 root root  434 Oct 12 09:28 example3.txt
-rw-r--r--. 1 root root  646 Oct 12 09:31 example4.txt
-rw-r--r--. 1 root root  521 Oct 12 09:32 example5.txt
-rw-rw-r--. 1 		    7.5K Oct 11 17:56 example.txt
drwxrwxr-x. 2 		       6 Oct 12 09:06 new folder
-rwxrwxrwx. 1 root root 7.3M Oct 11 16:36 test.txt

[root@lnx149]# find /home/Desktop -mmin -10
/home/Desktop
/home/Desktop/example2.txt
/home/Desktop/example3.txt
/home/Desktop/example4.txt
/home/Desktop/example5.txt

对当前路径下最近48小时内修改过的文件
find /home/Desktop -mtime -2

[root@lnx149]# ls -lh /home/Desktop/
total 7.3M
-rwxrwxrwx. 1 root root  345 Oct 12 09:09 example1.txt
-rwxrwxrwx. 1 root root  693 Oct 12 09:23 example2.txt
-rwxrwxrwx. 1 root root  434 Oct 12 09:28 example3.txt
-rw-r--r--. 1 root root  646 Oct 12 09:31 example4.txt
-rw-r--r--. 1 root root  521 Oct 12 09:32 example5.txt
-rw-r--r--. 1 root root  692 Oct 12 09:33 example6.txt
-rw-rw-r--. 1 		    7.5K Oct 11 17:56 example.txt
drwxrwxr-x. 2 		       6 Oct 12 09:06 new folder
-rwxrwxrwx. 1 root root 7.3M Oct 11 16:36 test.txt

[root@lnx149]# find /home/Desktop -mtime -2
/home/Desktop
/home/Desktop/test.txt
/home/Desktop/example.txt
/home/Desktop/new folder
/home/Desktop/example1.txt
/home/Desktop/example2.txt
/home/Desktop/example3.txt
/home/Desktop/example4.txt
/home/Desktop/example5.txt
/home/Desktop/example6.txt

文件大小

对当前路径下超过1M的文件
find /home/Desktop/ -size +1M -type f -print

对当前路径下小于500字节的文件
find /home/Desktop/ -size -500c -type f -print

对当前路径下空的文件/文件夹
find /home/Desktop empty

[root@lnx149]# ls -lh /home/Desktop/
total 7.3M
-rwxrwxrwx. 1 root root  345 Oct 12 09:09 example1.txt
-rwxrwxrwx. 1 root root  693 Oct 12 09:23 example2.txt
-rwxrwxrwx. 1 root root  434 Oct 12 09:28 example3.txt
-rw-r--r--. 1 root root  646 Oct 12 09:31 example4.txt
-rw-r--r--. 1 root root  521 Oct 12 09:32 example5.txt
-rw-r--r--. 1 root root  692 Oct 12 09:33 example6.txt
-rw-r--r--. 1 root root  904 Oct 12 09:34 example7.txt
-rw-rw-r--. 1 		    7.5K Oct 11 17:56 example.txt
drwxrwxr-x. 2 		       6 Oct 12 09:06 new folder
-rwxrwxrwx. 1 root root 7.3M Oct 11 16:36 test.txt

[root@lnx149]# find /home/Desktop/ -size +1M -type f -print
/home/Desktop/test.txt

[root@lnx149]# find /home/Desktop/ -size -500c -type f -print
/home/Desktop/example1.txt
/home/Desktop/example3.txt

[root@lnx149]# find /home/Desktop -empty
/home/Desktop/new folder

文件新旧

对当前路径下比 ’example5.txt’ 新的文件
find -newer ‘example5.txt’ -type f -print

对当前路径下比 ’example5.txt’ 旧的文件
find ! -newer ‘example5.txt’ -type f -print

对当前路径下比 ’example5.txt’ 新但比 ‘example7.txt’ 旧的文件
find . -newer ‘example5.txt’ ! -newer ‘example7.txt’ -type f -print

[root@lnx149 Desktop]# ls -lh .
total 7.3M
-rwxrwxrwx. 1 root root  345 Oct 12 09:09 example1.txt
-rwxrwxrwx. 1 root root  693 Oct 12 09:23 example2.txt
-rwxrwxrwx. 1 root root  434 Oct 12 09:28 example3.txt
-rw-r--r--. 1 root root  646 Oct 12 09:31 example4.txt
-rw-r--r--. 1 root root  521 Oct 12 09:32 example5.txt
-rw-r--r--. 1 root root  692 Oct 12 09:33 example6.txt
-rw-r--r--. 1 root root  904 Oct 12 09:34 example7.txt
-rw-r--r--. 1 root root  913 Oct 12 09:38 example8.txt
-rw-rw-r--. 1 		    7.5K Oct 11 17:56 example.txt
drwxrwxr-x. 2 		       6 Oct 12 09:06 new folder
-rwxrwxrwx. 1 root root 7.3M Oct 11 16:36 test.txt

[root@lnx149 Desktop]# find -newer 'example5.txt' -type f -print
./example6.txt
./example7.txt
./example8.txt

[root@lnx149 Desktop]# find ! -newer 'example5.txt' -type f -print
./test.txt
./example.txt
./example1.txt
./example2.txt
./example3.txt
./example4.txt
./example5.txt

[root@lnx149 Desktop]# find . -newer 'example5.txt' ! -newer 'example7.txt' -type f -print
./example6.txt
./example7.txt

文件名

对指定路径下同名文件进行全局检索并打印出其所在路径及子路径
find 查找范围 -name 文件名 -print

find . -name message -print
# 在当前路径下全局检索名为 `message` 的文件

find . -name ‘me**age' -print
# 在当前路径下全局检索 名字以 `me`开头和`age`结尾 的文件

文件夹

假设当前路径下有许多名字相近的文件夹(e.g. systemd, system-release, sysconfig),针对关键字 sys来检索相关文件夹及其路径。

find

使用 find 命令 12

对指定路径下同名文件夹进行全局检索并打印出其所在路径及子路径
find 查找范围 -name 文件夹名字 -type d

对指定路径下含有关键字的文件夹进行全局检索并打印出其所在路径及子路径

find 查找范围 -name 文件夹名字(正则表达式) -type d

find . -name syslog -type d
# 在当前路径下全局检索名为 `syslog` 的文件夹

find . -name ‘s*log' -type d
# 在当前路径下全局检索 名字以 `s`开头和`log`结尾 的文件夹


关键字

cat

参考 3

单个文件

  • 应用场景
    查询路径下有单个目标类型的文件(e.g txt
    查询的关键字为 ‘XY’,
    目的为查询含有该关键词的所在行及行数

  • 命令
cat -n path/*.txt | grep -a ‘XY'

cat -n Desktop/*.txt | grep -a ‘XY',即检索 Desktop 路径下的 *.txt 文件中含有 XY 关键词的所在行及其行数


  • 应用场景
    目的为查询含有该关键词的所在行后(After)的n行记录

  • 命令
cat -n path/*.txt | grep -a 'XY' -A 10

cat -n Desktop/file.txt | grep -a ‘XY' -A 10,即检索 Desktop 路径下的 file.txt 文件中含有 XY 关键词的所在行后10行的记录

  • 应用场景
    目的为查询含有该关键词的所在行前(Before)的n行记录

  • 命令
cat -n path/*.txt | grep -a 'XY' -B 10

cat -n Desktop/file.txt | grep -a ‘XY' -B 10,即检索 Desktop 路径下的 file.txt 文件中含有 XY 关键词的所在行前10行的记录

  • 应用场景
    目的为查询含有该关键词的所在行前后(Context)的n行记录

  • 命令
cat -n path/*.txt | grep -a 'XY' -C 10

cat -n Desktop/file.txt | grep -a ‘XY' -C 10,即检索 Desktop 路径下的 file.txt 文件中含有 XY 关键词的所在行前后10行的记录


多个文件

  • 应用场景
    查询路径下有单个或多个同类型的文件(e.g txt
    查询的关键字为 ‘XY’,
    目的为查询含有该关键词的所在行

  • 命令
cat -n path/*/*.txt | grep -a ‘XY'

cat -n Desktop/*/*.txt | grep -a ‘XY',即检索 Desktop 路径下的所有文件夹下的 txt 文件中含有 XY 关键词的所在行


find

对当前路径下所有txt文件进行关键字 ’Serial number’ 的全局检索
find . -type f -name ‘*.txt’ | xargs grep -a -i ‘Serial number’

[root@lnx149 lnx121_147]# find . -type f -name '*.txt' | xargs grep -a -i 'Serial number'
./C.txt:2021-09-18 18:49:16.304703: 3,5,48089,0,Serial number  
./C.txt:2021-09-18 19:02:25.824199: 3,5,50145,0,Serial number  
./C.txt:2021-09-18 19:10:55.478816: 3,5,51148,0,Serial number  

grep

如果要对二进制文件进行关键字抓去,可参考 4

单个文件

  • 应用场景
    查询某一个 shell 被执行后的输出结果中出现指定关键词的次数,
    查询的关键字为 ‘XY’,
    目的为查询出现过 ‘XY’ 关键字的所在行

  • 命令
conda list | grep XY

conda list | grep XY,即检索 conda list 命令的输出结果中出现了 XY 关键词的所在行。
如果关键词不止一个,请参考此处 5

多个文件

  • 应用场景
    查询路径下有单个或多个文件夹,且每个文件夹下有单个或多个同类型的文件(e.g txt),
    查询的关键字为 ‘XY’,
    目的为查询出现过 ‘XY’ 关键字的文件名及其所在路径和含有该关键词所在行

  • 命令
grep XY path/*/*.txt

grep XY /Desktop/*/*.txt,即检索 Desktop 路径下的所有文件夹下的 txt 文件中出现了 XY 关键词的文件名及其所在路径和含有该关键词所在行


行数

假设存在文件 xxx.txt,目标内容为某些行 6


cat命令

使用 cat 命令 7

对文件C.txt取头n行或尾n行内容,e.g. 头/尾50行

头50行
cat -n C.txt | head -n 50

尾50行
cat -n C.txt | tail -n 50


grep命令

使用 grep 命令 7

对文件C.txt取相应行内容

以 i 开头的行
grep -n “^i” C.txt

不以 i 开头的行
grep -v -n “^i” C.txt


head/tail命令

使用 headtail 命令 8

对文件C.txt取头n行或尾n行内容并重定向到目标路径 /home/Desktop 下的目标文件 content.txt

头50行
head -n 50 C.txt > /home/Desktop/content.txt
head 50 C.txt > /home/Desktop/content.txt

尾50行
tail -n 50 C.txt > /home/Desktop/content.txt
tail -50 C.txt > /home/Desktop/content.txt

第50行
tail -n +50 test.txt | head -1 > /home/Desktop/content.txt

第5~10行
tail -n +5 test.txt | head -6 > /home/Desktop/content.txt


sed

使用 sed 命令 [^sed1]

对文件C.txt取特定行数, e.g. 头/尾50行

头50行
sed -n ‘1,50p’ C.txt

第50行
sed -n ‘50p’ C.txt

第5~10行
sed -n ‘5,10p’ C.txt

第 3 行 和 5~7 行
sed -n ‘3p;5,7p’ C.txt

奇数行
sed -n ‘1~2p’ C.txt

偶数行
sed -n ‘0~2p’ C.txt

m~np:m 表示起始行;~2 表示:步长

5的倍数行
sed -n ‘0~5p’ C.txt

i 开头的行
sed -n ‘/^i/p’ C.txt

不以 i 开头的行
sed -n ‘/i/!p’ C.txt


awk

使用 awk 命令

对文件C.txt取特定行数, e.g. 头50行

头50行
awk ‘NR<51’ C.txt

尾行
awk ‘END {print}’ C.txt

第50行
awk ‘NR==50’ C.txt

第5~10行
awk ‘NR>4 && NR<11’ C.txt

第 3 行 和 5~7 行
awk ‘NR==3 || (NR>4 && NR<8)’ C.txt

奇数行
awk ‘NR%2!=0’ C.txt

偶数行
awk ‘NR%2==0’ C.txt
awk ‘!(NR%2)’ C.txt


参考文章

写本文时曾参考以下文章:

%todo:
如何通过sed命令在文件中包含某个关键字的指定行的上面或下面插入内容


  1. grep 模糊匹配_Linux模糊查找文件的N种方法,欢迎收藏和留言 ↩︎ ↩︎

  2. Linux查找文件、文件夹 ↩︎

  3. grep时提示:Binary file (standard input) matches grep只递归匹配文本文件,不匹配二进制文件中 ↩︎

  4. grep时提示:Binary file (standard input) matches grep只递归匹配文本文件,不匹配二进制文件中 ↩︎

  5. Linux: grep多个关键字“与”和“或” ↩︎

  6. Linux 打印文本部分行内容(前几行,指定行,中间几行,跨行,奇偶行,后几行,最后一行,匹配行) ↩︎

  7. Linux日志中查找关键字及其前后的信息实例方法 ↩︎ ↩︎

  8. linux取txt文件的前n行到另一个文件 ↩︎

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值