LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-01-2013, 09:14 PM   #1
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Rep: Reputation: 30
command grep -- howto Recursive subdirectory


I got simple question about command: grep
I try the man manual but not much help, I got following result

Code:
#grep -Rn "keyword" *
#grep -Rn "keyword" *.php
The first command can do what I want like recursive search SUB directory/folder
The second command can do what I want like only search .php script file type

But howto search SUB directory/folder AND file type=.php ?

Thank you for help in advance!

Last edited by fhleung; 01-01-2013 at 09:17 PM.
 
Old 01-01-2013, 09:44 PM   #2
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Quote:
$grep -ir "keyword" *
Will scan all files(of any type)in current directory & sub-directories in this current directory,which include .php extension files as well.

Example:

[user@machine ~]$ cat a
ravi
[user@machine ~]$ cat a.php
ravit

[user@machine ~]$grep -ir ravi *
a:ravi
a.php:ravit

Note: Please mark this post as Helpful,if it helps you.
 
Old 01-01-2013, 10:30 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
I try the man manual but not much help...
Read the man page once again, it has everything mentioned there.
Anyway, you could have tried -R or -d options with grep. Some examples are here:-
Code:
grep -R 'string' /path/to/dir
grep -R 'string' infile1 infile2 infile3...
grep -R 'string' *.php
 
Old 01-01-2013, 11:37 PM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
To my knowledge, it is not possible with grep alone; I can explain why that is the case (there are no directories matching *.php so it will not recurse into any directories) but I can not solve the puzzle with grep on it's own.

The below will do what you want
Code:
find . -name "*.php" -exec grep keyword {} \;
E.g. to find all c files that contain the main function
Code:
wim@aa0:~$ find . -name "*.c" -exec grep -Hi main {} \;
./forums/lq4175440522/lq4175440522_1.c:int main()
./forums/lq4175440522/lq4175440522_2.c:int main()
./forums/lq4175440522/lq4175440522_4.c:int main()
./programs/c/lq4175418035/lq4175418035_1.c:int main()
./programs/c/lq4175418035/lq4175418035.c:int main()
wim@aa0:~$
I've added -i to make it case-insensitive and -H to display the filename as well.

@shivaa
If there is a solution with grep only, please point us / me to the correct options.
 
1 members found this post helpful.
Old 01-02-2013, 04:16 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
When you use a globbing pattern as a filename, then the shell expands it before the command is executed, and you end up giving grep a fixed list of files to search.

Although if one of those expanded entries is a directory, and -r is used, it will descend into it recursively, as well as searching the other entries individually.

So use the -r option and pass it the starting directories that you want. You can control which files to look in with the --include/--exclude options.

(Note, BTW, that the globbing patterns supported by those options are separate from the shell's globbing, and so must be quoted so that the shell doesn't try to use them first).

Code:
grep -rn 'keyword' --include='*.php' */
After looking at man grep, see info grep for an even more detailed description on its use.

Last edited by David the H.; 01-02-2013 at 04:23 AM. Reason: some rewording + example
 
Old 01-03-2013, 01:29 AM   #6
trijit
Member
 
Registered: Sep 2010
Location: Kolkata
Distribution: Ubuntu
Posts: 35

Rep: Reputation: 3
Quote:
Originally Posted by fhleung View Post
I got simple question about command: grep
I try the man manual but not much help, I got following result

Code:
#grep -Rn "keyword" *
#grep -Rn "keyword" *.php
The first command can do what I want like recursive search SUB directory/folder
The second command can do what I want like only search .php script file type

But howto search SUB directory/folder AND file type=.php ?

Thank you for help in advance!
grep -irl "String" *.php should work.
 
Old 01-11-2013, 06:02 AM   #7
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
Dear all forum contributor / folks / companion

Thank you all. Please give me time to digest and I will respond.
 
Old 01-13-2013, 01:38 AM   #8
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
Quote:
The below will do what you want
Code:
find . -name "*.php" -exec grep keyword {} \;
If for the command: find alone, what it does seem search files with the input name = *.php like above code. Remember execute at root / as well as add *

So one of the difference between command find and grep was:
grep = search the keyword/content inside the file
find = search the file with filename equal input




I will try above full command soon. Thank you and please comment if there any mistake.

Last edited by fhleung; 01-18-2013 at 12:17 AM.
 
Old 01-18-2013, 05:43 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
I'll suggest one minor modification to the above command:

Code:
find . -name "*.php" -exec grep keyword {} +
When you use ';' to terminate the -exec command string it opens up a new command instance for every file found. But when you use '+' it will open up only a single instance (or at least as few as possible, depending on your system's maximum executing limit), and append all the filenames to it at once, in batch mode. This can save you time and processing overhead.

Just note that it's limited to commands that can accept a string of multiple filenames at the end of their argument lists, because the '{}' brackets must be the final argument when '+' is used (it will be replaced with the list of files matched). When using cp or mv, to give one particular example, you need to use the -t option to specify the destination directory before the files that will be moved.

'+' is POSIX-defined, so it should be available to all modern implementations of find.


Again though, gnu grep at least has the ability to filter the filenames it will search through built-in, so you can avoid the extra process. find is really only needed when you have to filter by criteria other than the name.
 
Old 01-18-2013, 06:49 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
If you want to grep .php files recursively in subfolders but not the current directory you could use "*/*.php" as the argument. As long as at least one subdirectory exists.
 
Old 01-18-2013, 12:34 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
For that matter, bash from v4+ also has recursive "globstar" pattern matching.

Code:
shopt -s globstar
grep 'keyword' **/*.php
**/ expands to a list of all subdirectories, plus the top directory.

I don't fully recommend at this time however, because it always follows symlinks. It works great as long as there are no self-or-parent referencing directory links, but if there are (e.g. ~/.wine/dosdevices) it will get stuck in a recursive loop and totally hose your shell session.
 
Old 05-12-2013, 12:18 AM   #12
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
Microsoft grep findstr

command grep also available in Microsoft Windows

findstr

Code:
findstr /S keyword *.java
 
  


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
Howto find out files that are changed less than 10 hours with grep command coolloo_djack Programming 1 03-20-2010 01:05 PM
recursive grep xpucto Solaris / OpenSolaris 2 05-29-2007 09:57 AM
How to use GZIP in recursive subdirectory RMLinux Linux - Newbie 4 08-08-2006 11:22 PM
Recursive grep jimieee Linux - General 5 10-06-2003 10:13 AM
grep file in the subdirectory juno Linux - General 3 09-30-2002 11:08 AM


All times are GMT -5. The time now is 02:24 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration