Richard Rublev Richard Rublev - 3 months ago 10
Linux Question

Why doesn't using grep -r -e filename find files named "filename"?

I want to search k1.sh, in all folders of my directory

ADU-07 WebInterface-JobTable_files
ADU-07 WebInterface-JobTable.html
ADU-07 WebInterface-SelfTestResults_files
ADU-07 WebInterface-SelfTestResults.html
meas_2016-07-13_20-22-00
meas_2016-07-13_20-25-13
meas_2016-07-13_20-29-43
meas_2016-07-13_20-33-43
meas_2016-07-13_20-37-43
meas_2016-07-13_20-54-43
meas_2016-07-13_21-46-43


When I try

grep -r -e "k1.sh" /home/milenko/SerradoMel/MT06


I got nothing.Why? How to check if my grep supports -r option?

Answer

grep checks files' contents, not files' names. You can test this like so:

mkdir -p foo.d
echo "non-matching" >foo.d/k1.sh
echo "k1.sh"        >foo.d/matching
grep -r -e k1.sh foo.d
rm -rf -- foo.d

The output of the above is:

foo.d/matching:k1.sh

...where foo.d/matching is the filename which was matched, and k1.sh is the content.

You'll see that it doesn't identify the file named k1.sh (but having the contents non-matching), but instead identifies the file named matching; this is because the documented purpose of grep is to look at files' contents, not files' names.


Instead, to find a file named k1.sh, use:

find /home/milenko/SerradoMel/MT06 -type f -name k1.sh