LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-21-2010, 06:08 AM   #1
boidi
Member
 
Registered: Jun 2010
Location: Bangalore
Posts: 48

Rep: Reputation: 15
how to find a text in a file?


in directory so many files are there. if we want to find some text in all the files, what is command is to be used.
 
Old 07-21-2010, 06:09 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
`grep` would be my first choice.
 
Old 07-21-2010, 06:56 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Rep: Reputation: 78
yes grep will do:

here are some examples:
Code:
grep "My text" *
will find "My text" in all files in your current folder

Code:
grep --color "My text" *
Same as above but will highlight your string when it finds it

Code:
grep -R --color -B4 -A2  "My text" *
Same as above but including all sub folder as well and will show 4 lines before each mach and 2 lines after the mach .

man grep for more help

Angel

Last edited by angel115; 07-22-2010 at 02:38 AM.
 
Old 07-21-2010, 12:13 PM   #4
skaushal_lk
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu, CentOS
Posts: 27

Rep: Reputation: 4
you can try this way also:

Code:
find .-name *.* | xargs grep "string"
 
Old 07-21-2010, 12:29 PM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by skaushal_lk View Post
you can try this way also:

Code:
find .-name *.* | xargs grep "string"
That won't even work as written. It seems like a very complex solution to this simple problem anyway.

Code:
find .
The "." is not needed if you want to find files in the current directory. You may omit it.

Code:
find .-name *.*
find: .-name: No such file or directory
To make this work you need a space between the "." and the "-".

The next problem you will face is that the "*.*" will be expanded by the shell, not the find command. As an example, assume the current directory contains three files as shown below:

Code:
ls -1
file1
file2
file3
The fully expanded find command will look like this:

Code:
find . -name
The reason no file names are included in the expansion is because none of the file names in the current directory contain the "." character. As far as the shell is concerned pattern "*.*" literally means "all file names containing ."

I could go on with all the problems with this find construction, but in deference to the OP I'll cut to the chase.

If you want to search for text in a single file use this.

Code:
grep 'text to search for' filename
Often you are uncertain whether certain letters are upper case or lower case, so use this:

Code:
grep -i 'text to search for' filename
You can search a group of files in a single directory by using a pattern instead of filename:

Code:
# search all ".txt" files in the current dir
grep -i 'text to search for' *.txt

# search all files in the current dir
grep -i 'text to search for' *
When specifying file names and patterns remember that file names are case sensitive on Linux!

Code:
grep -i 'text to search for' *.TXT
You can search all files within a directory tree, but it may take some time depending on how many files and how deeply nested they are:

Code:
grep -i -R 'text to search for' *.txt
Finally, remember that 'text to search for' is a regular expression, not a simple pattern. If you need to search for anything more complex than exact text matches then you should read up on regular expressions.

Code:
man grep
http://en.wikipedia.org/wiki/Regular_expression
http://www.regular-expressions.info/
http://google.com/search?q=regular%20expressions

Last edited by Telengard; 07-21-2010 at 12:48 PM. Reason: accidental premature submission
 
Old 07-21-2010, 12:48 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by skaushal_lk View Post
you can try this way also:

Code:
find .-name *.* | xargs grep "string"
What is *.* supposed to mean?!?

In Linux, there is no such thing as file extensions. Just use * instead.
 
Old 07-23-2010, 12:16 AM   #7
boidi
Member
 
Registered: Jun 2010
Location: Bangalore
Posts: 48

Original Poster
Rep: Reputation: 15
grep -i 'dsfas' * works fine for me.
thanq all
 
  


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
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 03:31 AM
find a Nth line in text file mahabooba Programming 2 07-25-2006 12:56 AM
Code to find out text file size Deepak Inbasekaran Programming 10 04-11-2006 05:52 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
Command to find file with text chamanrana Linux - Software 10 12-02-2004 11:47 AM


All times are GMT -5. The time now is 01:19 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