24

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?

2
  • 1
    If you tried to run this and there is a difference, then you know what the difference is already. What more can we tell you?
    – Ken White
    Apr 2, 2017 at 6:54
  • yes but I want to know why there is a difference
    – tn2000
    Apr 2, 2017 at 6:57

5 Answers 5

31

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.

4

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.

1
  • That's not what I see. In the second example, grep is matching the output from the find command, not the content of the files. Dec 23, 2021 at 21:25
1

When you have a path with txt in the directory name, the second command will find a match. When you do not want to match paths like txtfiles/allfiles.tgz and transactions/txtelevisions/bigscreen.jpg you will want to use the first.

0
0

The difference between the two is that in the first case, find is looking for files whose name (just name) matches the pattern.

In the second case, find is looking for all files of type 'f' and outputting their relative paths as strings. That result gets piped to grep, which filters the input strings to those matching the pattern. The pattern 'txt' will filter the filepath results for the pattern. 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.

-2

The first command will display files having txt in their name. Whereas the second command will highlight the lines of all the files having txt in their content.

1
  • This has already been mentioned in the other answers, for example here. When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.
    – Eric Aya
    Nov 11, 2021 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.