ack 可以仅根据文件名查找文件吗?

发布于 2024-12-08 23:54:06 字数 186 浏览 6 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我同意 find 是可行的方法,但您也可以使用 ack 轻松做到这一点:

ack -f | ack "string"

这里,“ack -f”递归地列出它将搜索的所有文件;通过管道将其传递给第二个 ack 命令以进行搜索。
ack -f 确实具有跳过二进制文件和目录的优点,即使没有任何更多参数;通常,“find”命令可以替换为更短的“ack”命令。

I agree find is the way to go, but you could also easily do it with ack:

ack -f | ack "string"

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.

堇色安年 2024-12-15 23:54:06

您可以使用find实用程序。像这样的事情:

find /path/to/look/in -name '*somestring*' -print

在某些系统上,如果省略路径,则使用当前目录。在其他系统上,您不能省略它,只需使用 . 代替当前目录。

另外,请阅读 man find 了解许多其他选项。

You can use find utility. Something like this:

find /path/to/look/in -name '*somestring*' -print

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.

吃颗糖壮壮胆 2024-12-15 23:54:06

如果您希望使用 ack 查找基本名称中包含 somestring 的所有文件,只需将 [^/]*$ 添加到正则表达式中,例如,

ack -g somestring[^/]*$

不幸的是,--color 将显示从 somestring 到文件名末尾的匹配;但可以使用 ack 执行您所要求的操作。

If you wish to use ack to find all files that have somestring in their basename, just add [^/]*$ to the regex, e.g.,

ack -g somestring[^/]*$

Unfortunately, --color will show the match from somestring to the end of the filename; but it is possible to do what you asked for using ack.

卸妝后依然美 2024-12-15 23:54:06

我还不能发表评论,否则我会将其添加到上面 @dmedvinsky 的答案中。您可能希望将他的方法与 @whaley 的对不同问题的回答结合起来,以过滤掉 .svn< /代码> 文件:

find /path/to/look/in -not -iwholename '*.svn*' -name '*somestring*' -print

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:

find /path/to/look/in -not -iwholename '*.svn*' -name '*somestring*' -print
中二柚 2024-12-15 23:54:06

“Jeffrey Aguilera”提供的答案应标记为正确答案。与 -g 选项一起,-w 选项非常方便。在下面的调用中,我有兴趣查找所有包含“001”的文件和目录的名称:

ack -w '001' -g
pghack/001.src/index.js
pghack/001.src/stl.js
migrations/001-initial.sql
...
expeditions/001/chords.mjs
expeditions/001/chords.html
expeditions/001/README.md

在我看来,求助于 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':

ack -w '001' -g
pghack/001.src/index.js
pghack/001.src/stl.js
migrations/001-initial.sql
...
expeditions/001/chords.mjs
expeditions/001/chords.html
expeditions/001/README.md

Resorting to find command is unnecessary and doesn't answer the OP's question, IMO.

黑色毁心梦 2024-12-15 23:54:06

这对我有用, 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文