smeeb smeeb - 2 months ago 6
Linux Question

Shell-fu to list all files in a directory that contain instance of a word or phrase

Linux here. I'm trying to run some kind of shell-fu command against an arbitrary directory on my file system, and have it spit out all the files (searching recursively) inside that directory that contain at least one instance of a particular (quoted) phrase. So something like this:

someMagicCommand ~/some/path/to/a/dir -phrase "How now brown cow"
Listing:
/home/myUser/path/to/a/dir/TheArsonist.txt
/home/myUser/path/to/a/dir/HasOddly.csv
/home/myUser/path/to/a/dir/subdir/yetanothersubdir/ShapedFeet.xml


Where those 3 files (
TheArsonist.txt
,
HasOddly.csv
, and
ShapedFeed.xml
) all contain at least one instance of `"How now brown cow" inside of them. Don't care about anything other than the file names and their paths (which can be absolute or relative to the target directory, no preferences). But I specifically just want paths + names (not content dumps).

I've tried all manners of
grep
and
find
variations but not even sure where to start here. Any ideas?

Answer

You can do this with a simple grep command

grep -rl "how now brown cow" /path/to/dir
Comments