Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the { } /dev/null \; when using find with -exec

I am trying to understand this line:

find $HOME -name "*.c" -exec grep "find this string" {} /dev/null \;

I understand the majority of it, but I am not sure about the /dev/null that appears after the {} and before the ;.

find locates every C program file, and then for each file, grep looks for the line that contains the string... and then is it sending all the errors to /dev/null?

like image 984
Brow Avatar asked Mar 05 '26 05:03

Brow


1 Answers

It's used to force grep to print the matching file name, only useful in greps that don't have a specific option to do so. Look:

$ cat file
1
2
3

$ grep 2 file
2

$ grep 2 file /dev/null
file:2

It used to be required to get that output but with GNU grep (and others?) these days you can do this instead:

$ grep -H 2 file
file:2

You may want to check the sell-by-date on your text book ;-).

like image 170
Ed Morton Avatar answered Mar 06 '26 18:03

Ed Morton