LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-08-2019, 11:40 AM   #1
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Rep: Reputation: 13
grep, recursive


The OS is Centos
Edit: I am reasonably sure I am using BASH. The command line prompt is the dollar sign.
Core question: How to grep recursively?

Edit: Post 4 looks like the best answer, worked for me

Prerequisites: I did look at the man page and as I read it, this should work. The same for several google searches. I clicked the Search box here, entered "grep", clicked Go, and absolutely nothing. I am in a government installation, go thorugh their firewalls, and that has been a problem in the past.

When I run a grep and the requested string is found in the current directory, each instance is returned. When I move up a directory and add a -r or a -R, nothing is returned. The response is: No such file or directory. This is the command:
Code:
grep -r Open *.cpp
I have tried this in the lower level directory.
Code:
find . -name *.cpp
It finds the files. So do this, again, in the lower level directory where the files do reside:
Code:
find . -name *.cpp | grep Open
The response is nothing. I am using this to find the ultimate source of some declarations. Sometimes I must go through multiple include files before finding what I really need.
Your advise please.

Last edited by bkelly; 03-11-2019 at 02:54 PM. Reason: note where to find the answer.
 
Old 03-08-2019, 12:07 PM   #2
dc.901
Senior Member
 
Registered: Aug 2018
Location: Atlanta, GA - USA
Distribution: CentOS/RHEL, openSuSE/SLES, Ubuntu
Posts: 1,005

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
I am confused; what exactly are you looking for? File/directory with certain characters in their name?
Or looking for certain characters within file(s)?
 
Old 03-08-2019, 12:23 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,078

Rep: Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364
Code:
grep -r Open <dir>
is the correct syntax.
grep -r will not really work with files (like *.cpp).
find will return filenames, you will not find Open in filenames
 
Old 03-08-2019, 12:28 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
I use find with the -exec option and then perform the grep there:
Code:
find . -name "*.cpp" -exec grep <string> {} /dev/null \;
 
Old 03-08-2019, 12:31 PM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,162

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
This is defined in my .bashrc for doing recursive grep. Maybe others have something better:

Code:
gfind () 
{ 
    if [ -z "$1" ]; then
        return;
    fi;
    find . -type f -exec grep -H "$1" \{} \; 2> /dev/null
}
 
Old 03-08-2019, 12:43 PM   #6
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by dc.901 View Post
I am confused; what exactly are you looking for? File/directory with certain characters in their name?
Or looking for certain characters within file(s)?
I am looking for the files that have the text "Open" in them. I do the search again for the .h files to find where something is declared.
When I don't find the declaration, move up one directory level and do a recursive search on the .h files.
If not found one level up, go another level up.
 
Old 03-08-2019, 12:46 PM   #7
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by pan64 View Post
Code:
grep -r Open <dir>
...grep -r will not really work with files (like *.cpp). ...
I do not understand what you intend by that. My current understanding is that in Linux the characters ".cpp" on the end of the file name are just a few more characters on the end. They have no effect on the significance of the file. I use that to limit my search. I look for how it is used by specifying *.cpp and for where it is declared by using *.h. From your post I infer that might not be valid.

Last edited by bkelly; 03-08-2019 at 12:48 PM.
 
Old 03-08-2019, 12:57 PM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,078

Rep: Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364
Quote:
Originally Posted by bkelly View Post
I do not understand what you intend by that. My current understanding is that in Linux the characters ".cpp" on the end of the file name are just a few more characters on the end. They have no effect on the significance of the file. I use that to limit my search. I look for how it is used by specifying *.cpp and for where it is declared by using *.h. From your post I infer that might not be valid.
recursive means walk inside dirs. If you specify files there are no dirs to go thru.
So
Code:
grep -r 'pattern' <dir[s]>'
or
grep 'pattern' <filelist>
 
Old 03-08-2019, 01:01 PM   #9
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by rtmistler View Post
I use find with the -exec option and then perform the grep there:
Code:
find . -name "*.cpp" -exec grep <string> {} /dev/null \;
That almost worked. I removed the quotes around the file specifier and it did work.

Code:
find . -name *.cpp -exec grep <string> {} /dev/null \;
[/QUOTE]

That seems to be strange.
I neglected to mention that I think the shell is bash, the command prompt is the dollar sign.
That will work. But, gosh, seems like the -r or the -R should work. Those would be much easier.

EDIT: IMPORTANT: I have discovered that my quotes problem was caused by manually copying the command into Libre Writer as a place to save it. When I did that edit, Writer replaced the Linux style quotes (right phrase) with open and close document style quotes. Those don't work so well. I am now sure the original was and is fine. What was back up in #4

Last edited by bkelly; 03-11-2019 at 02:53 PM.
 
Old 03-08-2019, 01:07 PM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by bkelly View Post
Code:
grep -r Open *.cpp
i think the problem is that the shell expands the '*.cpp', so grep effectively gets a list of filenames, and does not recurse anywhere.
it would be interesting to test if the results are different whether there's cpp files in the current directory or not.

btw, i'm pretty sure
Code:
grep -r Open
would work on recent grep verisons, however it would then search ALL files recursively.

if you need to limit this to cpp files, you need to use one of the find/grep constructs suggested by others here.

Quote:
Originally Posted by bkelly View Post
That seems to be strange.
I neglected to mention that I think the shell is bash, the command prompt is the dollar sign.
That will work. But, gosh, seems like the -r or the -R should work. Those would be much easier.
strange for you, business as usual for others.
maybe would be easier for you, more difficult for others.
btw, the dollar sign says nothing about the shell you use.

Last edited by ondoho; 03-08-2019 at 01:11 PM.
 
Old 03-08-2019, 01:36 PM   #11
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by pan64 View Post
recursive means walk inside dirs. If you specify files there are no dirs to go thru.
So
Code:
grep -r 'pattern' <dir[s]>'
or
grep 'pattern' <filelist>
I don't agree with that. By specifying a file limitation, such as *.cpp, to me, that means to conduct the search but limit it to the files specified. I think it should work not only in the current directory, but with the -r or -R, work in all the subdirectories.

As noted, and as I read the man page, it should work.
 
Old 03-08-2019, 01:57 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
My grep only worked using the -r and then only the search string.
Code:
$ grep -r <pattern>
The directory is managed by Git and because of this, there is information which grep found that I don't need to see.

The only way I could get it to work was to use */*.c or */*/*.c, thus forcing me to know the depth of the tree, and/or overlooking whether or not there were lesser or greater depth files in there which were being overlooked.

This reminded my why, so very long ago, I forsook the use of grep -r
 
Old 03-08-2019, 02:02 PM   #13
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by bkelly View Post
I don't agree with that. By specifying a file limitation, such as *.cpp, to me, that means to conduct the search but limit it to the files specified. I think it should work not only in the current directory, but with the -r or -R, work in all the subdirectories.

As noted, and as I read the man page, it should work.
you don't agree and you think it should... yet that is not how grep works. your computing life will continue to be unpleasant if you cannot adjust to how programs work...

of course you can always file a bug against grep's disagreeable behavior and see what the devs have to say about it.
if you do please make sure to share a link here!

btw:
Quote:
Originally Posted by man grep
If no FILE is given, recursive searches examine the working directory
weirdly phrased, but there it is.
 
Old 03-08-2019, 03:33 PM   #14
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by ondoho View Post
you don't agree and you think it should... yet that is not how grep works. your computing life will continue to be unpleasant if you cannot adjust to how programs work...

of course you can always file a bug against grep's disagreeable behavior and see what the devs have to say about it.
if you do please make sure to share a link here!

btw:
weirdly phrased, but there it is.
Aww, if only I could copy paste from my working computer to here. Regardless, my man page, from Centos, states:

Quote:
grep searches the named input FILEs (or standard input if not files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
Those words do not imply that if a file specification is provided then the recursive option will not work. I scrolled down some to look at the -r and -R option and neither one had anything to say about file specifications and how the recursion would not be performed when an file specifier is used. Therefore, I claim that to say it should work is a reasonable conclusion. Stating that does not mean I cannot adjust, but it is a valid observation.
 
Old 03-08-2019, 04:37 PM   #15
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by bkelly View Post
The OS is Centos
Edit: I am reasonably sure I am using BASH. The command line prompt is the dollar sign.
Core question: How to grep recursively?
It
Code:
grep -r Open *.cpp
I have tried this in the lower level directory.
Code:
find . -name *.cpp
The thing is that "*.cpp" is evaluated BY the shell in the current directory only, before grep gets passed the commandline.
So grep will search recursively for the .cpp files that have the same name as the ones IN that current directory (no others).
As you went one dir upwards there were no .cpp files in that directory thus grep didn't have any filenames to search for.
You will need something like find to expand your (quoted) wildcards.

You must always remember that most programs in Linux (and in fact Unix) do not do their own wildcard (*, ? etc) handling, they let the shell (bash or whatever) do that for them, so "*.cpp" means: all of the .cpp files in the current directory (and */*.cpp all of them ONE single directory deeper). The find program is one of the exceptions, it can do its own wildcard expansion, as long as the wildcard IS protected from the shell (by backslash or quotes). So
Code:
find . -name "*.cpp"
will find all .cpp files in the whole tree of directories from the current one downwards and the -exec will then execute the grep command ON them.

Last edited by ehartman; 03-08-2019 at 04:39 PM.
 
  


Reply



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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Sorting recursive 'ls' and 'grep' SirTristan Linux - Newbie 5 03-13-2008 02:39 PM
non Recursive query and Recursive query prashsharma Linux - Server 1 06-27-2007 09:33 AM
recursive grep xpucto Solaris / OpenSolaris 2 05-29-2007 09:57 AM
Recursive grep jimieee Linux - General 5 10-06-2003 10:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:12 PM.

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