Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a string in multiple files using grep

I have a folder with sub-folders inside, all have many types of files. I want to search for a word inside the .css-files. I am using Windows 7 and I have grep.

How I can use grep to :

  1. Find pattern and print it
  2. Give file name (and path) if pattern found
like image 923
Leo92 Avatar asked Sep 07 '12 19:09

Leo92


People also ask

How do you grep 2 patterns at a time?

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path. The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.

How do you search for a string in all files recursively in Linux?

To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.


1 Answers

Actually you don't need find. Just use:

grep -R --include=*.css -H pattern .

this will recurse and look for all *.css in subdirectories, while -H will show the filename.

like image 179
Bitwise Avatar answered Oct 17 '22 16:10

Bitwise