LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Easy shell line look for string (https://www.linuxquestions.org/questions/programming-9/easy-shell-line-look-for-string-912088/)

g_paschoal 11-05-2011 06:46 PM

Easy shell line look for string
 
Hi Guys..

I have a folder with a lot of subfolders and php files and no php files...

I need a command that will list all files in this main folder and look for a specific string inside of just the .php files.

what's the best way to do that?

Thanks

TB0ne 11-05-2011 06:58 PM

Quote:

Originally Posted by g_paschoal (Post 4516873)
Hi Guys..
I have a folder with a lot of subfolders and php files and no php files... I need a command that will list all files in this main folder and look for a specific string inside of just the .php files.

Read the man page for grep...type in "man grep" at the command line.
Code:

grep -H "<whatever string to look for>" *.php

foodown 11-05-2011 07:02 PM

One command won't do it ... You'll need to write a small script. It may even be able to be one line long, but you'll need a small set of commands.

You'll need ls, cat, grep, and, depending on your methodology, perhaps additionally wc and others.

You will probably also need man.

Cedrik 11-05-2011 07:51 PM

You can try :
Code:

grep -rln <string> /path/to/folder | grep .php
(but not very efficient)

Maybe:
Code:

find /path/to/folder -name "*.php" -print0 | xargs -0 grep -l <string>

TB0ne 11-06-2011 09:23 AM

Quote:

Originally Posted by foodown (Post 4516881)
One command won't do it ... You'll need to write a small script. It may even be able to be one line long, but you'll need a small set of commands.

You'll need ls, cat, grep, and, depending on your methodology, perhaps additionally wc and others.
You will probably also need man.

Why wouldn't grep work, to find a string inside of a set of files?? That's exactly what it DOES.

Grep can recurse folders, but (based on the OP's question), they wanted to look inside the 'main folder' for all PHP files, and look in them for a string. That's grep...the -H flag returns which file names contain the string.

Cedrik 11-06-2011 09:26 AM

Quote:

Originally Posted by TB0ne (Post 4517264)
but (based on the OP's question), they wanted to look inside the 'main folder'

Oh I overlooked that :study:


All times are GMT -5. The time now is 05:20 PM.