Difference Between grep and find Command in UNIX

The main difference between grep and find command in UNIX is that the grep is a command that helps to search content and display them according to the user-specified regular expression while the find command helps to search and locate files according to the given criteria.

UNIX is an older operating system that performs a variety of tasks to support the proper functioning of the entire system. It controls hardware devices. Moreover, it handles process management, memory management, task scheduling and many more. UNIX provides a powerful Command Line Interface (CLI). The user can enter the commands to this interface to achieve the required tasks. There are a numerous number of commands with different options and grep and find are two vital command utilities.

Key Areas Covered

1. What is grep
     – Definition, Examples
2. What is find
     – Definition, Examples
3. What is the Difference Between grep and find Command in UNIX
    – Comparison of Key Differences

Key Terms

grep, find, UNIX

Difference Between grep and find Command in UNIX- Comparison Summary

What is grep

The grep command allows uers to scan documents and to represent the results according to the required format.

Difference Between grep and find Command in UNIX

Figure 1: UNIX terminal to enter commands

The syntax is as follows.

grep “literal string” <filename>

Refer below examples.

grep “apple” file1.txt

This command displays all the lines with the word “apple” in the file1.

It is also possible to scan multiple documents as follows.

grep “apple” file1.txt  file2.txt

This command will search the word “apple” in both files.

If the user requires finding all the file formats, he can use the command as follows.

grep  “apple”  file1.*

This will search the “apple” string in files of all formats.

For case-insensitive searching, it is possible to use the option ‘i’ as follows.

grep  -i  “apple”  file1.txt

Furthermore, below command display the 4 lines after the matching string.

grep  -A 4  “apple” file1.txt

Similarly, the following command displays 3 lines before the matching string.

grep  -B 3 “apple” file1.txt

Those are few examples of grep command.

Overall, this command allows searching the given regular expression and displaying the matching lines.

What is find

The find command helps to search and locate files. It will search files inside the directory according to the given search criteria.

The syntax for find command is as follows.

find <path> <search criteria> <action>

The below command will find all the files.

find –type  f

The below command will find all the directories.

find  –type  d

Refer further examples.

find .  –name file1.txt

The above command will find file1.txt in the current directory.

find  /home/abc  –name  test1.txt

The above command finds the test1.txt file in the abc directory.

find /home/abc –name  *.txt

This command finds all the .txt files in the abc directory.

find  /home/abc –iname test1.txt

The above command finds the test1.txt file in the abc directory ignoring the case.

find /home/abc – name test1.*

This command displays all the test1 files with all formats.   

The following command searches the file1 in the entire system starting from the root directory.

sudo find  /  -name file1

Those are a few examples of find command. Overall, find allows to search files.

Difference Between grep and find Command in UNIX

Definition

The grep is a command line utility for searching plain text data sets for lines matching a regular expression. The find is a command line utility that searches one or more directory trees of a file system and locates files based on the user-specified criteria. This is the main difference between grep and find command in UNIX.

Usage

A grep command helps to scan documents and to represent the result according to a specified format whereas a find command helps to search and locate files in the system.

Syntax

Command grep has the syntax,  grep “literal string” <filename> while the command find follows the syntax, find <path> <search criteria> <action>.

Conclusion

The difference between grep and find command in UNIX is that the grep is a command that helps to search content and display them according to the user-specified regular expression while the find command helps to search and locate files according to the given criteria.

Reference:

1. Learn Grep with 15 Amazing Examples, Linux Tutorial, 1 June 2014, Available here.
2. Linux Command Line Tutorial For Beginners 29 – Find Command, ProgrammingKnowledge, 30 Dec. 2016, Available here.
3. Learn Find Command in 5 Minutes, Linux Tutorial, 1 June 2014, Available here.

Image Courtesy:

1. “Version 7 Unix SIMH PDP11 Emulation DMR” By Huihermit – Own work (CC0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply