randomness2077
randomness2077

Reputation: 1139

find string from the file in somewhere

I want to find a string from some file in subdirectory. Like we are in bundle/. and in bundle/ there are multiple subdirectories and multiple txt files I want to do something like

  find . -type f -exec grep "\<F8\>" {} \;

want to get the file where it contain string < F8 >

this command does work, find the string, but never return filename I hope anyone can give me a better solution to this, like display filename along with the line containing that string

Upvotes: 0

Views: 432

Answers (4)

user849425
user849425

Reputation:

Personally I find it's easier just to use ack. (ack-grep is the name used in Ubuntu's repos to keep from confusing it with another software with the same name.) It's available in most major repositories.

The command would be ack -a "<F8>" (or ack-grep -a "<F8>" in Ubuntu). The -a option is to search all file types.

Example:

testfile

Line 1
Line 2
Line 3
<F8>
<F9>
<F10>
Line 4
Line 5
Line 6

Output:

$ ack -a "<F8>"
testfile
4:<F8>

Upvotes: 0

ghoti
ghoti

Reputation: 46846

grep -rl '<F8>' .

The -r option tells grep to search recursively through directories starting at .

The -l option tells it to show you just the filename that's matched, not the line itself.

Your output will look something like this:

./thisfile
./foo/bar/thatfile

If you want to limit this to only one file, append | head -1 to the end of the line.

If you want output like:

./thisfile:My text contains the <F8> string
./foo/bar/thatfile:didn't remember to press the <F8> key on the

then you can just leave off the -l option. Note that this output is not safe to parse, as filenames may contain colons, and colons in filenames are not escaped in grep's output.

Upvotes: 2

offex
offex

Reputation: 1885

This should list out all the files and line numbers that match:

grep -nR '<F8>' *

Upvotes: 0

univerio
univerio

Reputation: 20518

You can use grep by itself.

grep -r '<F8>' .

Upvotes: 1

Related Questions