How to Use Grep for Searching Data in Files and Directories

How to Use Grep for Searching Data in Files and Directories

Last Blog Review

In the last, blog we understood Linux permission table, on how to use the numeric value to simply give read, write, and execute permission to the user, group’s and other user’s using a single command with chmod i.e. change mode use.

Regular Expression →

Regular Expression are special characters which help search data, matching complex patterns Grep :

The grep filter searches a file or directory for a particular pattern of characters and display all lines that contain that pattern.

Let’s understand how to user Grep Practically →

  • Lets create a file in which we will write 2 words Devops and devops. And we will find this word using grep. So, we are using command "grep -r word-to-find directory" we are using -r means recursive because we are finding in the directory.
i-0c8dd7a6179312bf1 (EC2)
-------------------------
ubuntu@ip-------:~$ whoami
ubuntu
ubuntu@ip-------:~$ vim fil.txt
ubuntu@ip-------:~$ cat fil.txt 
Devops
devops
ubuntu@ip-------:~$ grep -r Devops /home/ubuntu/
/home/ubuntu/fil.txt:Devops
ubuntu@ip-------:~$ grep -r devops /home/ubuntu/
/home/ubuntu/fil.txt:devops
ubuntu@ip-------:~$ grep -r dev* /home/ubuntu/
/home/ubuntu/fil.txt:devops
ubuntu@ip-------:~$
  • Say we have written Devops and devops in the file. But we want to do case insensitive search i.e. even if we write Devops/devops/deVops anything we should get the word devops from the given path.
i-0c8dd7a6179312bf1 (EC2)
-------------------------
ubuntu@ip-------:~$ grep -ri devops /home/ubuntu/
/home/ubuntu/fil.txt:Devops
/home/ubuntu/fil.txt:devops
ubuntu@ip-------:~$ grep -r -i Devops /home/ubuntu/
/home/ubuntu/fil.txt:Devops
/home/ubuntu/fil.txt:devops
ubuntu@ip-------:~$ grep -r -i DEVops /home/ubuntu/
/home/ubuntu/fil.txt:Devops
/home/ubuntu/fil.txt:devops
ubuntu@ip-------:~$

Conclusion →

So, in this blog we have understood how to user “GREP“ effectively, as it is a powerful utility used for searching and manipulating text within files. Its primary function is to scan each line of input and print lines that match a specified pattern, making it essential for tasks involving data extraction and analysis.

💡
Loved this post? Share it with your friends! If you have any questions or thoughts, leave a comment below – I’m looking forward to hearing your perspective. Don’t forget to subscribe for more content delivered directly to you. Stay tuned for more tips, ideas, and inspiration coming soon!