Can ack find files based on filename only?

2019-03-08 19:35发布

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?

标签: filenames ack
5条回答
甜甜的少女心
2楼-- · 2019-03-08 19:55

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

查看更多
爷的心禁止访问
3楼-- · 2019-03-08 19:57

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.

查看更多
够拽才男人
4楼-- · 2019-03-08 20:15

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.

查看更多
冷血范
5楼-- · 2019-03-08 20:18

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.

查看更多
戒情不戒烟
6楼-- · 2019-03-08 20:20

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
查看更多
登录 后发表回答