乐闻世界logo
搜索文章和话题

How can I use grep to show just filenames on Linux?

1个答案

1

In Linux systems, you can use the -l option (lowercase L) of the grep command to display only the file names that contain the search pattern. This option causes grep to output only the file names after finding matching lines, rather than the default line content.

For example, if you want to search for all file names containing the word "example" in the current directory, you can use the following command:

bash
grep -l "example" *

This command checks all files in the current directory. For each file, grep verifies whether it contains the word "example". If it does, it outputs the file name.

Additionally, if you want to search recursively in all subdirectories and display only file names, you can add the -R option (for recursive search):

bash
grep -Rl "example" .

Here, . represents the current directory. This command searches for files containing "example" in the current directory and all its subdirectories, listing the paths of these files.

This method is highly useful, especially when searching for specific content across a large number of files, as it enables you to quickly identify files containing that content.

2024年8月15日 00:40 回复

你的答案