How to accurately search for file content in Linux?

You can use the following command in the Linux system to accurately search for file contents:

  1. search for a specific piece of text or pattern in a file or program.
grep "要查询的内容" 文件名

For example, to search for lines containing “hello” in the file example.txt, you can use the following command:

grep "hello" example.txt
  1. locate
find 目录路径 -type f -exec grep -l "要查询的内容" {} \;

The directory path is where to search for files, -type f indicates to only search for files and not directories, -exec grep -l “content to search for” {} \; means to execute the grep command in each found file to search for the specified content.

For example, to search for files containing “hello” in the current directory, you can use the following command:

find . -type f -exec grep -l "hello" {} \;

These commands can be adjusted based on the needs to meet various query requirements.

bannerAds