简体   繁体   中英

How to list files which contain specific string using linux commands

我需要使用 linux 命令从包含特定字符串的目录中列出文件。

Use the command

grep -inr <string> /path/to/directory

The -inr option has three parts:

  • i to ignore case sensitivity,
  • n to display the line numbers in for each matched result, and
  • r to recursively read all files under each directory.

Let say your string is in the environment variable STR and you search in directory dir/ . You can do:

find dir/ -type f -name "*${STR}*"

If you search a file containing the string:

find dir/ -type f -exec grep -l $STR {} \;

But this is explained here

You could do

grep -nr <string> <path to the directory>

This will print the name of the files with the line number containing the string

If you want to list file name(s) with a particular string then do

find <path to the directory> -iname <string_pattern> -type f

This command will find files with matching string. However, I added little more extra, exec will list the files.

command

find <your/dir/> -name "string_to_search*" -type f -exec ls -l {} \;

or this command will list all the c files in your directory.

find <your/dir/> -name "*.c" -type f -exec ls -l {} \;

Using find command to find the matching string inside the file.

find  -type f | xargs grep 'text-to-find-here'

Here / means search in all dirs

find / -type f | xargs grep 'text-to-find-here'

or

grep -r "string to be searched"  /path/to/dir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM