Grep From Files and Display the File Name On Linux or Unix System

You can use the grep command with the -H option to display the file name along with the matching lines. For example, to search for a pattern in multiple files and display the file name, you can run the following command:

grep -H 'pattern' file1 file2 file3

In this example, pattern is the string you are searching for, and file1, file2, and file3 are the names of the files you want to search. The -H option is used to display the file name with each matching line.

If you want to search for a pattern in all the files in the current directory and its subdirectories, you can use the -r option to search recursively:

grep -rH 'pattern' .

In this example, the . specifies the current directory and the -r option is used to search recursively in all subdirectories. The -H option is used to display the file name with each matching line.

Leave a Comment