Bash – Two find commands comparison

bashfindshell

Recently I received this find one-liner, but I'm not able to explain where the difference of the below two comes from:

Example 1

[root@centos share]# find . -exec grep -i "madis" {} /dev/null \;

./names:Madison Randy:300:Product Development

Example 2

[root@centos share]# find . -exec grep -i "madis" {} \;

Madison Randy:300:Product Development

As you can see, in the first one there is the specific file this string derives from and so far I'm really not able to find out why this is happening.

Best Answer

You are telling grep to search 2 locations. grep only shows the full location if multiple locations are searched.

For example

touch /tmp/herp /tmp/derp
cd /tmp
echo "foo" > herp
echo "foo" > derp

Notice how if I search just 1 file, grep omits the file name

grep -i "foo" /tmp/herp
foo

But if I specify multiple search locations, grep says where it found each match

grep -i "foo" herp derp
/tmp/derp:foo
/tmp/herp:foo

Adding the /dev/null is it tricking grep into printing out the full path, by providing 2 arguments.