Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show grep result with complete path or file name

Tags:

linux

grep

bash

I need to know the complete file path when I grep.

I use commands like

cat *.log | grep somethingtosearch 

Now what I need to show the result with complete file path from where the matched result were taken out.

Help anyone?

like image 554
Özzesh Avatar asked Dec 14 '12 08:12

Özzesh


People also ask

How do I grep a file path?

If you want to find in all files of a directory, you can use grep -r /path/to/directory .

How do I grep a filename in a directory?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

How do you grep for filename recursively?

If you are using GNU grep, then you can use the following: grep -ir --include "*. cpp" "xyz" . The command above says to search recursively starting in current directory ignoring case on the pattern and to only search in files that match the glob pattern "*.

Which of the following option in grep command display name of the file if the pattern is found?

Display the file names that matches the pattern : We can just display the files that contains the given string/pattern. 4. Checking for the whole words in a file : By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.


2 Answers

Assuming you have two log-files in:

  • C:/temp/my.log
  • C:/temp/alsoMy.log

cd to C: and use:

grep -r somethingtosearch temp/*.log 

It will give you a list like:

temp/my.log:somethingtosearch temp/alsoMy.log:somethingtosearch1 temp/alsoMy.log:somethingtosearch2 
like image 149
Kaadzia Avatar answered Sep 29 '22 14:09

Kaadzia


I fall here when I was looking exactly for the same problem and maybe it can help other.

I think the real solution is:

cat *.log | grep -H somethingtosearch 
like image 21
Sergio Avatar answered Sep 29 '22 12:09

Sergio