15.7 Getting a List of Matching Files with grep -lNormally when you run grep (27.1 ) on a group of files, the output lists the filename along with the line containing the search pattern. Sometimes you want to know only the names of the files, and you don't care to know the line (or lines) that match. In this case, use the -l (lowercase letter "l") option to list only filenames where matches occur. For example, the command: % searches the files for a line containing the string
You don't have to create a file list, though.
You can insert the output of a grep
directly into a command line
with command substitution.
For example, to edit only the subset of files containing % grep -l is also good for shell programs that need to check whether a file contains a particular string. The traditional way to do that test is by throwing away grep 's output and checking its exit status: if grep If somefile is huge, though, grep has to search all of it. Adding the grep -l option saves time because grep can stop searching after it finds the first matching line. - , |
|