Linux之ack命令

ack是比grep好用的文本搜索工具
ack命令安装
> yum install -y ack

命令特点
  • 默认搜索当前工作目录
  • 默认递归搜索子目录
  • 忽略元数据目录,比如.svn,.git,CSV等目录
  • 忽略二进制文件(比如pdf,image,coredumps)和备份文件(比如foo~,*.swp)
  • 在搜索结果中打印行号,有助于找到目标代码
  • 能搜索特定文件类型(比如Perl,C++,Makefile),该文件类型可以有多种文件后缀
  • 高亮搜索结果
  • 支持Perl的高级正则表达式,比grep所使用GNU正则表达式更有表现力。
相比于搜索速度,ack总体上比grep更快。ack的速度只要表现在它的内置的文件类型过滤器。在搜索过程中,ack维持着认可的文件类型的列表,同时跳过未知或不必要的文件类型。它同样避免检查多余的元数据目录。
命令参数
  • -n, 显示行号
  • -l/L, 显示匹配/不匹配的文件名
  • -c, 统计次数
  • -v, invert match
  • -w, 词匹配
  • -i, 忽略大小写
  • -f, 只显示文件名,不进行搜索.
  • -h, 不显示名称
  • -v, 显示不匹配
在当前目录递归搜索单词”eat”,不匹配类似于”feature”或”eating”的字符串:
> ack -w eat

搜索有特殊字符的字符串’$path=.’,所有的元字符(比如’$',’.')需要在字面上被匹配:
> ack -Q '$path=.' /etc

除了temp目录,在所有目录搜索use单词
> ack use --ignore-dir=temp

只搜索包含'main'单词的Python文件,然后通过文件名把搜索结果整合在一起,打印每个文件对应的搜索结果
> ack--python--group -w main

Linux之ack命令
文章图片

ack支持的文件类型
> ack --help-types--[no]actionscript .as .mxml --[no]ada.ada .adb .ads --[no]asm.asm .s --[no]asp.asp --[no]aspx.master .ascx .asmx .aspx .svc --[no]batch.bat .cmd --[no]cc.c .h .xs --[no]cfmx.cfc .cfm .cfml --[no]clojure.clj .cljs .edn .cljc --[no]cmakeCMakeLists.txt; .cmake --[no]coffeescript .coffee --[no]cpp.cpp .cc .cxx .m .hpp .hh .h .hxx --[no]csharp.cs --[no]css.css --[no]dart.dart --[no]delphi.pas .int .dfm .nfm .dof .dpk .dproj .groupproj .bdsgroup .bdsproj --[no]elisp.el --[no]elixir.ex .exs --[no]erlang.erl .hrl --[no]fortran.f .f77 .f90 .f95 .f03 .for .ftn .fpp --[no]go.go --[no]groovy.groovy .gtmpl .gpp .grunit .gradle --[no]gsp.gsp --[no]haskell.hs .lhs --[no]hh.h --[no]hpp.hpp .hh .h .hxx --[no]html.htm .html .xhtml --[no]jade.jade --[no]java.java .properties --[no]js.js --[no]json.json --[no]jsp.jsp .jspx .jspf .jhtm .jhtml --[no]kotlin.kt .kts --[no]less.less --[no]lisp.lisp .lsp --[no]lua.lua; first line matches /^#!.*\blua(jit)?/ --[no]make.mk; .mak; makefile; Makefile; Makefile.Debug; Makefile.Release --[no]matlab.m --[no]objc.m .h --[no]objcpp.mm .h --[no]ocaml.ml .mli .mll .mly --[no]parrot.pir .pasm .pmc .ops .pod .pg .tg --[no]perl.pl .pm .pod .t .psgi; first line matches /^#!.*\bperl/ --[no]perltest.t --[no]php.php .phpt .php3 .php4 .php5 .phtml; first line matches /^#!.*\bphp/ --[no]plone.pt .cpt .metadata .cpy .py --[no]python.py; first line matches /^#!.*\bpython/ --[no]rakeRakefile --[no]rr.R --[no]rst.rst --[no]ruby.rb .rhtml .rjs .rxml .erb .rake .spec; Rakefile; first line matches /^#!.*\bruby/ --[no]rust.rs --[no]sass.sass .scss --[no]scala.scala --[no]scheme.scm .ss --[no]shell.sh .bash .csh .tcsh .ksh .zsh .fish; first line matches /^#!.*\b(?:ba|t?c|k|z|fi)?sh\b/ --[no]smalltalk.st --[no]smarty.tpl --[no]sql.sql .ctl --[no]stylus.styl --[no]swift.swift; first line matches /^#!.*\bswift/ --[no]tcl.tcl .itcl .itk --[no]tex.tex .cls .sty --[no]ts.ts .tsx --[no]tt.tt .tt2 .ttml --[no]vb.bas .cls .frm .ctl .vb .resx --[no]verilog.v .vh .sv --[no]vhdl.vhd .vhdl --[no]vim.vim --[no]xml.xml .dtd .xsd .xsl .xslt .ent .wsdl; first line matches /<[?]xml/ --[no]yaml.yaml .yml

获取包含CFLAG关键字的Makefile的文件名
> ack --make CFLAG

ack查找my.cnf文件
> ack -f /etc/ | ack my.cnf //或者 > ack -g my.cnf /etc/

【Linux之ack命令】原文链接:https://rumenz.com/rumenbiji/...
微信公众号:入门小站

    推荐阅读