6

I have a lot of files and I want to find where is MYVAR.

I'm sure it's in one of .yml files but I can't find in the grep manual how to specify the filetype.

Share a link to this question
CC BY-SA 3.0
14
grep -rn --include=*.yml "MYVAR" your_directory

please note that grep is case sensitive by default (pass -i to tell to ignore case), and accepts Regular Expressions as well as strings.

Share a link to this answer
CC BY-SA 3.0
1
1

You don't give grep a filetype, just a list of files. Your shell can expand a pattern to give grep the correct list of files, though:

$ grep MYVAR *.yml

If your .yml files aren't all in one directory, it may be easier to up the ante and use find:

$ find -name '*.yml' -exec grep MYVAR {} \+

This will find, from the current directory and recursively deeper, any files ending with .yml. It then substitutes that list of files into the pair of braces {}. The trailing \+ is just a special find delimiter to say the -exec switch has finished. The result is matching a list of files and handing them to grep.

Share a link to this answer
CC BY-SA 3.0
0
find . -name \*.yml -exec grep -Hn MYVAR {} \;
Share a link to this answer
CC BY-SA 3.0
0

If all your .yml files are in one directory, then cd to that directory, and then ...

grep MYWAR *.yml

If all your .yml files are in multiple directories, then cd to the top of those directories, and then ...

grep MYWAR `find . -name \*.yml`

If you don't know the top of those directories where your .yml files are located and want to search the whole system ...

grep MYWAR `find / -name \*.yml`

The last option may require root privileges to read through all directories.

The ` character above is the one that is located along with the ~ key on the keyboard.

Share a link to this answer
CC BY-SA 3.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.