Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-21-2010, 05:08 AM   #1
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, 05:09 AM   #2
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, 05:56 AM   #3
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 532

Rep: Reputation: 78
yes grep will do:

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

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

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


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

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

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

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

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

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

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:

ls -1
The fully expanded find command will look like this:

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.

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

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:

# 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!

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:

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.

man grep

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

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

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-22-2010, 11:16 PM   #7
Registered: Jun 2010
Location: Bangalore
Posts: 48

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


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

All times are GMT -5. The time now is 08:27 PM.

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