LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2011, 06:46 PM   #1
g_paschoal
Member
 
Registered: Oct 2009
Posts: 131

Rep: Reputation: 15
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
 
Old 11-05-2011, 06:58 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,616

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by g_paschoal View Post
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
 
Old 11-05-2011, 07:02 PM   #3
foodown
Member
 
Registered: Jun 2009
Location: Texas
Distribution: Slackware
Posts: 611

Rep: Reputation: 221Reputation: 221Reputation: 221
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.
 
0 members found this post helpful.
Old 11-05-2011, 07:51 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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>

Last edited by Cedrik; 11-05-2011 at 07:58 PM.
 
1 members found this post helpful.
Old 11-06-2011, 09:23 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,616

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by foodown View Post
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.
 
Old 11-06-2011, 09:26 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by TB0ne View Post
but (based on the OP's question), they wanted to look inside the 'main folder'
Oh I overlooked that
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
csh Shell Script: String Concatenation, how do i add a new line character? vxc69 Programming 1 05-04-2009 07:51 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration