To display the name of the file when using the grep
command to search for a pattern in multiple files in Unix or Linux, you can use the -H
or --with-filename
option.
For example, to search for the pattern "test" in the files file1.txt
and file2.txt
and display the name of the file before each matching line, you can use the following command:
grep -H "test" file1.txt file2.txt
This will search for the pattern "test" in the files file1.txt
and file2.txt
, and print the name of the file before each matching line.
You can also use the -l
or --files-with-matches
option to print only the names of the files that contain the pattern, without printing the matching lines. For example:
grep -l "test" file1.txt file2.txt
This will print the names of the files file1.txt
and file2.txt
if they contain the pattern "test," without printing the matching lines.