LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Listing files whose contains contain a string, and are of a particular extension? (https://www.linuxquestions.org/questions/linux-newbie-8/listing-files-whose-contains-contain-a-string-and-are-of-a-particular-extension-806186/)

SirTristan 05-05-2010 06:35 PM

Listing files whose contains contain a string, and are of a particular extension?
 
I believe that the following can be used to locate files that contain the contents 'SomeString' or 'SomeOtherString':
Code:

find /home/username/public_html -type f -print0 | xargs -0 egrep -l 'SomeString|SomeOtherString'
How would I limit this to searching for the text 'SomeString' or 'SomeOtherString', but only if the file has extension .php, .inc or .js?

Also - can someone help explain details of what piping to xargs does here? I don't understand how this command actually works.

pixellany 05-05-2010 07:00 PM

ls <pathname> | egrep '(php|inc|js)$' | xargs <...etc.(your code)..>

ls -R if you need it to drill down

grail 05-05-2010 07:17 PM

Code:

find /home/username/public_html -type f -name '*.php' -o -name '*.inc' -o -name '*.js' -print0 | xargs -0 egrep -l 'SomeString|SomeOtherString'

or

find /home/username/public_html -type f -name '*.php' -o -name '*.inc' -o -name '*.js' -exec egrep -l 'SomeString|SomeOtherString' {} \;


sumeet inani 05-06-2010 01:10 AM

When you
run a command then add a "| xargs command2". The results of command1 will be passed to
command2, possibly on a line−by−line basis

-print0 means null char after file name not newline as we have also given -0 option for xargs.
-l means only filename mentioned not matching content within it.
egrep means extended pattern in grep (same as grep -E)

i suggest
find . -type f | grep -i "file_extension_you_want" | egrep -i 'pattern1|pattern2'
Though not short , but more lucid.
by the way -i means ignore case


All times are GMT -5. The time now is 10:16 PM.