简体   繁体   English

find 和 grep 有什么区别?

[英]What is the difference between find with grep?

What is the difference between these command:这些命令有什么区别:

find . –type f –name '*txt*' 

and

find . –type f | grep 'txt'

I tried to run this and there is a difference but I want to know why?我试图运行它,但有区别,但我想知道为什么?

The Major difference is FIND is for searching files and directories using filters while GREP is for searching a pattern inside a file or searching process(es) 主要区别是FIND用于使用过滤器搜索文件和目录,而GREP用于搜索文件内的模式或搜索过程。

FIND is an command for searching file(s) and folder(s) using filters such as size , access time , modification time. FIND是使用过滤器(如大小,访问时间,修改时间)搜索文件和文件夹的命令。
The find command lists all of the files within a directory and its sub-directories that match a set of filters. find命令列出目录中的所有文件及其与一组过滤器匹配的子目录。
This command is most commonly used to find all of the files that have a certain name. 此命令最常用于查找具有特定名称的所有文件。

To find all of the files named theFile.txt in your current directory and all of its sub-directories, enter: 要查找当前目录及其所有子目录中名为theFile.txt的所有文件,请输入:

find . -name theFile.txt -print

To look in your current directory and its sub-directories for all of the files that end in the extension .txt , enter: 要在当前目录及其子目录中查找以扩展名.txt结尾的所有文件,请输入:

find . -name "*.txt" -print

GREP :(Globally search a Regular Expression and Print) GREP :(全球搜索正则表达式和打印)

Searches files for a specified string or expression. 搜索文件以查找指定的字符串或表达式。

Grep searches for lines containing a specified pattern and, by default, writes them to the standard output. Grep搜索包含指定模式的行,默认情况下将它们写入标准输出。

grep myText theFile.txt

Result : Grep will print out each line contain the word myText . 结果:Grep将打印出每行包含单词myText

In your first example, you are using the find utility to list the filenames of regular files where the filename includes the string txt . 在第一个示例中,您使用find实用程序列出常规文件的文件名,其中文件名包含字符串txt

In your second example, you are using the find utility to list the filenames of regular files and feeding the resultant filenames via a pipe to the grep utility which searches the contents of each file for the string txt . 在第二个示例中,您使用find实用程序列出常规文件的文件名,并通过管道将结果文件名提供给grep实用程序,该实用程序在每个文件的内容中搜索字符串txt Each time the string is found, the corresponding line of the file is outputted. 每次找到该字符串时,都会输出该文件的相应行。

When you have a path with txt in the directory name, the second command will find a match. 如果目录名中有txt的路径,则第二个命令将找到匹配项。 When you do not want to match paths like txtfiles/allfiles.tgz and transactions/txtelevisions/bigscreen.jpg you will want to use the first. 当你不想匹配像txtfiles/allfiles.tgztransactions/txtelevisions/bigscreen.jpg你会想要使用第一个。

The difference between the two is that in the first case, find is looking for files whose name (just name) matches the pattern.两者之间的区别在于,在第一种情况下, find 正在寻找名称(仅名称)与模式匹配的文件。

In the second case, find is looking for all files of type 'f' and outputting their relative paths as strings.在第二种情况下, find 正在查找所有类型为“f”的文件并将它们的相对路径作为字符串输出。 That result gets piped to grep, which filters the input strings to those matching the pattern.该结果通过管道传送到 grep,grep 将输入字符串过滤为与模式匹配的字符串。 The pattern 'txt' will filter the filepath results for the pattern.模式 'txt' 将过滤模式的文件路径结果。 Importantly, the second case will include filepaths that match anywhere in the path, not just in the filename.重要的是,第二种情况将包括匹配路径中任何位置的文件路径,而不仅仅是文件名。 The first case will not do that.第一种情况不会这样做。

The first command will display files having txt in their name.第一个命令将显示名称中包含txt 的文件。 Whereas the second command will highlight the lines of all the files having txt in their content.而第二个命令将突出显示内容中包含txt的所有文件的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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