Can ack find files based on filename only?
Asked Answered
R

6

29

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?

Relationship answered 8/10, 2011 at 18:26 Comment(0)
O
26

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.

Opportune answered 8/10, 2011 at 18:33 Comment(2)
This is the answer. Unless you're using the filetype detection features of ack, find is the way to go.Murraymurre
This is not an answer to the question. Question is about how to do it with ack. There is at least one use case, where this is useless - consider someone using ack on windows - there your answer won't work.Unlash
P
50

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.

Pedicure answered 6/3, 2013 at 20:31 Comment(3)
Find is faster.Troup
ack -f | rg "string"Charybdis
This answer would include matches in the path (directory names, which the question wanted to exclude) as well as in the filename.Abase
O
26

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.

Opportune answered 8/10, 2011 at 18:33 Comment(2)
This is the answer. Unless you're using the filetype detection features of ack, find is the way to go.Murraymurre
This is not an answer to the question. Question is about how to do it with ack. There is at least one use case, where this is useless - consider someone using ack on windows - there your answer won't work.Unlash
W
7

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.

Whew answered 27/7, 2016 at 2:57 Comment(0)
M
0

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
Manned answered 22/3, 2014 at 18:53 Comment(0)
I
0

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.

Immerge answered 7/9, 2023 at 19:32 Comment(0)
S
-2

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

Snakebird answered 12/4, 2012 at 19:29 Comment(1)
This appears to search file contents. That's not what the question asked for.Demented

© 2022 - 2024 — McMap. All rights reserved.