Linux查找文件、目录、单词、短语

  • locate:

    • locate filename:搜索文件和目录的名称
    • locate -i filename:不区分大小写的搜索文件名和目录
  • grep :

    • grep want_to_find filename:搜索文件总包含想找的单词

    • want_to_find 单词中间有特殊符号的时候,可以加’’,告诉shell正在搜索一个字符串,使用""则表示要使用shell变量

    • grep -R want_to_find *:搜索多个目录的结果

    • grep -R want_to_find * > want_to_find .txt:把搜索的过多的结果放到want_to_find.txt中

    • grep -R want_to_find * --color=auto:把搜索出来的单词变成彩色

    • grep -i want_to_find *:找出不区分大小写的单词

    • grep -w want_to_find *:在文件中搜索完整的单词

    • grep -n want_to_find *:显示搜索单词在文件的行数

    • tail info.log | grep -B 3 "want_to_find " --color=auto 找含有want_to_find 字符的哪一行的前3行(before)

    • tail info.log | grep -A 3 "want_to_find " --color=auto 找含有want_to_find 字符的哪一行的后3行(after)

    • tail info.log | grep -C 3 "want_to_find " --color=auto 找含有want_to_find 字符的前后的完整的上下文信息(context)

    • ls -l | grep -n want_to_find --color=auto 可以把找到的含有want_to_find 字符的那一行的行号打印出来

    • ls -l | grep -v do_not_want_to_find 可以把不含有do_not_want_to_find 的结果打印出来

    • grep -il -l want_to_find filename_path/* 可以把含有do_not_want_to_find 的文件名称打印出来

    • grep -c 广告 info.log 把info.log中出现的广告的次数打印出来

    • ls -l | grep 196[6-7] | grep -v live 在某个搜索结果中搜索单词

猜你喜欢

转载自blog.csdn.net/Litside/article/details/82761861