LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to find a text in a file? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-a-text-in-a-file-821220/)

boidi 07-21-2010 05:08 AM

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.

GrapefruiTgirl 07-21-2010 05:09 AM

`grep` would be my first choice.

angel115 07-21-2010 05:56 AM

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

skaushal_lk 07-21-2010 11:13 AM

you can try this way also:

Code:

find .-name *.* | xargs grep "string"

Telengard 07-21-2010 11:29 AM

Quote:

Originally Posted by skaushal_lk (Post 4040923)
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

MTK358 07-21-2010 11:48 AM

Quote:

Originally Posted by skaushal_lk (Post 4040923)
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.

boidi 07-22-2010 11:16 PM

grep -i 'dsfas' * works fine for me.
thanq all


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