0

I'm in a bash terminal. I'm cd'd to a folder (web application folder)

I'm looking for a file. I know it exists. I don't know under which sub sub.. folder. All I know is that inside the file these words appear: "carousel" "javascript" "chart"

How can I find this file?

american-ninja-warrior
  • 7,397
  • 11
  • 46
  • 80
  • 1
    Possible duplicate of [How to find all files containing specific text on Linux?](http://stackoverflow.com/questions/16956810/how-to-find-all-files-containing-specific-text-on-linux) – Benjamin W. Jan 15 '17 at 04:34

3 Answers3

2

Try to run this command if you want to find the file containing any of these words:

grep -rE 'foo1|foo2' .

If you want to find the file containing all of these words,

try this:

grep -r 'foo1' .|grep 'foo2'

If you want to find multiple strings in file on different lines,

you can use

grep -rl 'foo1' .|xargs grep -l 'foo2'

-r

For each directory operand, read and process all files in that directory, recursively. Follow symbolic links on the command line, but skip symlinks that are encountered recursively. Note that if no file operand is given, grep searches the working directory.

-E

Use pattern as the pattern. If this option is used multiple times or is combined with the -f (--file) option, search for all patterns given. (-e is specified by POSIX.)

You can read GNU Grep 2.27 to learn more about grep. Hope this helps.

McGrady
  • 10,869
  • 13
  • 47
  • 69
  • I did upvote because this is the right "direction" but please note OP is looking for a file containing ALL those words (not any of them) – mauro Jan 15 '17 at 04:58
  • @mauro Thank you for reminding me.I updated my answer. – McGrady Jan 15 '17 at 05:41
0

To search from current directory downward through all directories try this:

find . -type f -exec egrep 'carousel|javascript|chart' {} \; -print

This will find ALL text files that contain your chosen words. If you know a specific file extension you want to search for you can modify it like so to search for Python files:

find . -name '*.py' -type f -exec egrep 'carousel|javascript|chart' {} \; -print
ChuckB
  • 522
  • 4
  • 10
0

If you search all the files with carousel and javascript and chart you can try :

find . -type f -exec sh -c 'grep -q carousel $1 && grep -q javascript $1 && grep -q chart $1 && echo $1' sh {} \;

Example :

$ cat a.txt
carousel
javascript
chart

$ cat b.txt
carousel
chart

Result :

./a.txt
V. Michel
  • 1,599
  • 12
  • 14