ack 可以仅根据文件名查找文件吗?
使用 ack (有时打包为 ack-grep)我知道我可以通过执行以下操作找到包含特定字符串的路径 :ack -g somestring
但是,如果我只想要文件名中包含“somestring”的文件怎么办?
Using ack (sometimes packaged as ack-grep) I know that I can find paths that contain a specific string by doing:ack -g somestring
But what if I only want files which have "somestring" in their filenames?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我同意 find 是可行的方法,但您也可以使用 ack 轻松做到这一点:
这里,“ack -f”递归地列出它将搜索的所有文件;通过管道将其传递给第二个 ack 命令以进行搜索。
ack -f 确实具有跳过二进制文件和目录的优点,即使没有任何更多参数;通常,“find”命令可以替换为更短的“ack”命令。
I agree find is the way to go, but you could also easily do it with ack:
Here, "ack -f" recursively lists all the files it would search; pipe that to the second ack command to search through that.
ack -f does have the advantage of skipping over binaries and directories even without any more arguments; often, then a "find" command could be replaced by a much shorter "ack" command.
您可以使用
find
实用程序。像这样的事情:在某些系统上,如果省略路径,则使用当前目录。在其他系统上,您不能省略它,只需使用
.
代替当前目录。另外,请阅读
man find
了解许多其他选项。You can use
find
utility. Something like this:On some systems, if you omit the path, current directory is used. On other systems you can't omit it, just use
.
for current directory instead.Also, read
man find
for many other options.如果您希望使用
ack
查找基本名称中包含somestring
的所有文件,只需将[^/]*$
添加到正则表达式中,例如,不幸的是,
--color
将显示从somestring
到文件名末尾的匹配;但可以使用ack
执行您所要求的操作。If you wish to use
ack
to find all files that havesomestring
in their basename, just add[^/]*$
to the regex, e.g.,Unfortunately,
--color
will show the match fromsomestring
to the end of the filename; but it is possible to do what you asked for usingack
.我还不能发表评论,否则我会将其添加到上面 @dmedvinsky 的答案中。您可能希望将他的方法与 @whaley 的对不同问题的回答结合起来,以过滤掉
.svn< /代码> 文件:
I can't comment yet, or else I'd add this to @dmedvinsky's answer above. You may want to combine his approach with @whaley's answer to a different question in order to filter out
.svn
files:“Jeffrey Aguilera”提供的答案应标记为正确答案。与
-g
选项一起,-w
选项非常方便。在下面的调用中,我有兴趣查找所有包含“001”的文件和目录的名称:在我看来,求助于
find
命令是不必要的,并且不能回答OP的问题。The answer provided by 'Jeffrey Aguilera' should be marked as the correct answer. Along with the
-g
option, the-w
option comes in pretty handy. In the invocation below, I am interested in find the names of all files and directories that have '001':Resorting to
find
command is unnecessary and doesn't answer the OP's question, IMO.这对我有用, ack -all "somestring" ,其中 ack --all "somestring" 将搜索包含该字符串的所有文件。 ack "somestring" 将仅搜索具有该字符串的文件。如果您想添加某种文件类型以像 ruby 文件一样永久搜索,或者您可以在主目录中创建 .ackrc 文件,请添加以下行 --type-add=ruby=.haml,.rake,.rsel 。乔什
this works for me, ack -all "somestring" where as ack --all "somestring" will search all files that contain that string. ack "somestring" will only search files with that string. if you wanted to add some file type to search permanently like ruby files or you can create .ackrc file in your home directory, add the fallowing line --type-add=ruby=.haml,.rake,.rsel . JOsH