Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between find with grep?

Tags:

find

command

unix

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?

like image 810
tn2000 Avatar asked Apr 02 '17 06:04

tn2000


People also ask

How do you grep and find?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

What is difference between find and locate in Linux?

Locate searches for files from a database that is created automatically. This database contains the list of files in a file system and their associated paths. So essentially locate simply searches through a file and returns a match. Find on the other hand searches through files in the file system in real time.

Why do we use grep?

The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.

What does find command do in Linux?

The Linux find command is one of the most important and frequently used command command-line utility in Unix-like operating systems. The find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.


2 Answers

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 is an command for searching file(s) and folder(s) using filters such as size , access time , modification time.
The find command lists all of the files within a directory and its sub-directories that match a set of filters.
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:

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:

find . -name "*.txt" -print

GREP :(Globally search a Regular Expression and Print)

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 myText theFile.txt

Result : Grep will print out each line contain the word myText.

like image 112
TheSprinter Avatar answered Oct 21 '22 21:10

TheSprinter


In your first example, you are using the find utility to list the filenames of regular files where the filename includes the string 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 the pipe (a list of filenames, one per line) for the string txt. Each time this string is found, the corresponding line (which is a filename) is outputted.

like image 40
fpmurphy Avatar answered Oct 21 '22 21:10

fpmurphy