Ask Your Question
0

Grep: How can I only show file names that contain a pattern?

asked 2023-04-26 15:34:23 +0000

wagner gravatar image

updated 2023-04-26 15:34:44 +0000

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-26 15:35:14 +0000

wagner gravatar image

To use grep to only show file names that contain a specific pattern, you can use the -l (lowercase "L") option. This option tells grep to only print the names of the files that contain the pattern, rather than printing the matching lines themselves.

Here's an example:

grep -l "pattern" *.txt

bash Copy code This will search for the pattern "pattern" in all files with the ".txt" extension in the current directory, and will only print the names of the files that contain the pattern.

Note that if you want to search recursively through all files in a directory and its subdirectories, you can use the -r option:

grep -rl "pattern" /path/to/directory

This will search for the pattern "pattern" in all files in the specified directory and its subdirectories, and will only print the names of the files that contain the pattern.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

1 follower

Stats

Asked: 2023-04-26 15:34:23 +0000

Seen: 32 times

Last updated: Apr 26 '23