LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 06-07-2014, 02:14 PM   #1
karthik.rajashekaran
LQ Newbie
 
Registered: Jun 2014
Posts: 8

Rep: Reputation: Disabled
Find a file in linux that contains String "Hello CS6823"


How to find a file in the Linux box that contains the string “Hello
CS6823” minus the quotes

Say we don't know which directory and which file name does it contain the String "Hello CS6823"

The below command can give output only if you know the directory

grep –H –r “Hello CS6823” /etc/
 
Old 06-07-2014, 03:23 PM   #2
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,053

Rep: Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881
Before I start, did the man page help on this subject?
 
Old 06-07-2014, 03:24 PM   #3
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Quote:
grep –H –r “Hello CS6823” /etc/
-H is the default when doing recursive

Code:
grep -r "Hello CS6823" / 2>/dev/null
 
Old 06-07-2014, 03:48 PM   #4
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
You can also use the Find File/Folder utility in your file manager. In KDE it's in either Dolphin or Konqueror. You just set the "Named" parameter as * (be sure to check hidden files and look in subfolders) and set the Look in: parameter as "/" and then in the contents tab you can put in whatever content you wish to look for in the files.
jdk
 
Old 06-07-2014, 07:27 PM   #5
karthik.rajashekaran
LQ Newbie
 
Registered: Jun 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
the above code didnt give me the result

I also tried below one
find -name *.txt |grep -r "Hello CS6823"

find -name *.txt gives me all the text files in the directory .. So i could find one in /etc/findme.txt

but if i do grep along with it , it doesnt search it
 
Old 06-07-2014, 10:21 PM   #6
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,433

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
Code:
find -name *.txt |grep -r "Hello CS6823"
That command causes the list of file names output by find to be piped to grep. As those file names do not contain the string "Hello CS6823", then there is no output.

Perhaps this is what you want
Code:
find . -name "*.txt" -exec grep -l "Hello CS6823" '{}' \;
A more robust version that will not be tripped up by filenames containing spaces is
Code:
find . -name "*.txt" -print0 | xargs -0I '{}' grep -l "Hello CS6823" '{}'

Last edited by allend; 06-07-2014 at 10:29 PM.
 
Old 06-07-2014, 11:16 PM   #7
karthik.rajashekaran
LQ Newbie
 
Registered: Jun 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
When I tried with below command . I got the output as shown in snap1 attachment

find . -name "*.txt" -exec grep -1 "Hello CS6823" '{}' 2>/dev/null \;

It doesnt show me which file contains that string "Hello CS6823"
Attached Thumbnails
Click image for larger version

Name:	snap1.JPG
Views:	5
Size:	99.4 KB
ID:	15690  
 
Old 06-07-2014, 11:18 PM   #8
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,433

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
The argument to grep is lowercase 'L' not the digit '1'.
 
Old 06-07-2014, 11:23 PM   #9
karthik.rajashekaran
LQ Newbie
 
Registered: Jun 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
amazing .. thanks a lot allend
 
Old 06-07-2014, 11:26 PM   #10
karthik.rajashekaran
LQ Newbie
 
Registered: Jun 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi

When i try to run the command from the home directory it doesn't find as below screenshot

But if i do cd / which goes to root directory and run it finds the result
Attached Thumbnails
Click image for larger version

Name:	snap1.JPG
Views:	5
Size:	26.3 KB
ID:	15691  
 
Old 06-07-2014, 11:32 PM   #11
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,433

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
You can specify the top directory where the find command is to start looking.
e.g. This will start in the root directory
Code:
find / -name "*.txt" -print0 | xargs -0I '{}' grep -l "Hello CS6823" '{}'
Previously I used '.' , which is the current directory.

Last edited by allend; 06-07-2014 at 11:34 PM.
 
Old 06-07-2014, 11:35 PM   #12
karthik.rajashekaran
LQ Newbie
 
Registered: Jun 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Super thanks a ton .. its interesting tons to learn
 
  


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
[SOLVED] Replace a string @CURRANGE("***","***") to @CURRANGE("xxx","xxx") in a file mavadikarmayur Linux - Newbie 3 03-26-2012 09:32 AM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 07:14 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 04:07 AM
using java to find the location of the "\" char in a string caged Programming 8 02-03-2004 11:42 AM


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