Using grep to search inside files

One very important tool that Unix provides is the grep command, which processes files line by line and prints out any matching the pattern you have set for it.

Syntax

grep [OPTIONS] PATTERN [FILE...]

Let's say you want to find where file.html is containg the word 'mywebsite.com' you would run the command (from the folder it is in)

grep "mywebsite.com" file.html

If grep finds it, it will output the entire line it is in, making it easier and faster to analyze your files.

To highlight the phrase with a color, you can use the --color option.
To view the line it was found at, you would need to use the -n option.
To do a case insensite server, -i would need to be used.

grep -i -n --color "mywebsite.com" file.html

To use a file wildcard and search all files sharing something in common (like extension, for example), you can use the '*'

grep -i -n --color "mywebsite.com" *.html

This would output any line in any html file in that folder containing mywebsite.com

If you want to do it recursively and search all subdirectories, you would need to add the -r option.

grep -r "mywebsite.com" *.html

Adding -c would give you the number of times the pattern was found, while -l would print the files with matches.

  • grep
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

What happens if I go over my bandwidth limit?

You will get a notification when you've reached 90% of your allocated bandwidth. If you reach the...

How do I add more bandwidth?

There are two ways you can add more bandwidth.1.) You can upgrade your account to add more...

How does DDoS protection work?

We provide DDoS filtering up to 10 Gbps included with all of our VPS plans at all locations. If...

Do you create backups of the VPS?

We do a snapshot backup of every VPS to dedicated backup server. This snapshot backup is for...

How do i set reverse DNS (PTR) on my ip address?

In order to set rDNS on your IP please open a ticket to our support department and we will apply...