The grep command in Linux is one of the most commonly used and powerful tools for searching text and patterns within files or command output. It allows users to quickly locate lines that match a given string or regular expression, making it indispensable for developers, system administrators, and DevOps engineers.
Whether you’re reviewing system logs, filtering configuration files, or debugging application output, grep offers flexibility and speed in handling large volumes of data. Mastering the grep command can significantly streamline tasks like monitoring logs, troubleshooting issues, and automating scripts on your Kamatera server.
Log in to your server
- Start by opening your terminal on your Linux machine or accessing your cloud instance via SSH.
In this guide, we are using an Ubuntu server. Here’s our tutorial on how to deploy a Kamatera server. Once your Kamatera server is up and running, log in using your assigned username and password via SSH.
Basic Syntax of grep: grep [options] pattern [file…]
- pattern: The text or regex you are searching for
- file: The file(s) to search through
- Search for a word in a file: To search for the word “Kamatera” in a log file, run the command below.
Command:
grep "Kamatera" myfile.txt
Note: Replace the word and file with the actual word and name of the file you want to search for.
- Ignore case sensitivity: To search without case sensitivity, use -i.
Command:
grep -i "kamatera" myfile.txt
Note: Replace the word and file with the actual word and name of the file you want to search for.
- Display line numbers: Use -n to display line numbers of matched lines.
Command:
grep -n "rates" myfile.txt
Note: Replace the word and file with the actual word and name of the file you want to search for.
- Search recursively: To search in all files under a directory.
Command:
grep -r "rates" myfile.txt
Note: Replace the word and file with the actual word and name of the file you want to search for.
- Use regular expressions: Use special characters for pattern matching. For example, to find lines starting with “Failed”.
Command:
grep "^Kamatera" myfile.txt
Note: Replace the word and file with the actual word and name of the file you want to search for.
- Invert the match: To display lines that do not match the pattern, use –v.
Command:
grep -v "rates" myfile.txt
Note: Replace the word and file with the actual word and name of the file you want to search for.
- Count matches: To count the number of matching lines, use –c.
Command:
grep -c "interest" myfile.txt
- Combine options: You can combine multiple options, for example:
Command:
grep -in "interest" myfile.txt
- This command will search for the word “rates” in all .txt files in the current directory and print the names of the files where the word is found, not the matching lines themselves.
Command:
grep -l "rates" *.txt
- This command will search for the word “kametera” in file.txt and return only the lines where “kamatera” appears as a complete word, not as part of another word like “kamateraserver”.
Command:
grep -w "kamatera" myfile.txt
- This command searches myfile.txt and prints all lines where the line starts with “rates”.
Command:
grep "^rates" myfile.txt
- This command searches for lines in myfile.txt that end with the word “rates” followed by exactly one character (like punctuation) and nothing else after that.
Command:
grep "rates.$" myfile.txt
- This command uses multiple -e options to search for multiple patterns in the same file.
- -e “Kamatera”: Matches lines containing “Kamatera” (with capital K)
- -e “kamatera”: Matches lines containing “kamatera” (lowercase k)
Command:
grep -e "Kamatera" -e "kamatera" myfile.txt
Note: This command is useful when:
- You want to search for more than one specific pattern.
- Patterns differ in case or spelling, and you don’t want to use -i.
- This command searches for running processes that match “process_name” using the ps and grep commands together.
Command:
ps aux | grep "process_name"
- This command searches for the word “rates” in myfile.txt and displays:
- The matching line
- 3 lines before and 3 lines after the match
- -C stands for “context”, so -C 3 gives 3 lines of context around each match.
Command:
grep -C 3 "rates" myfile.txt
- This command performs a recursive search for the word “rates” in files, while excluding .log files from the search.
Command:
grep --exclude="*.log" -r "rates" myfile.txt
- This command searches myfile.txt for any line that contains at least one digit (0 through 9).
Command:
grep "[0-9]" myfile.txt
- This command searches recursively in the current directory (.) for files named myfile.txt, and then runs grep “rates” on each matching file.
Command:
find myfile.txt -type f -exec grep "rates" {} +
- This command searches for lines in myfile.txt that exactly match the word rates.
Command:
grep -x "rates" myfile.txt
- This command searches myfile.txt for lines containing “kamatera”, and:
- -i: Makes the search case-insensitive (so it matches Kamatera, kamatera, KAMATERA, etc.)
- -B 4: Shows 4 lines before each match for context
Command:
grep -i -B 4 "kamatera" myfile.txt
- This command searches myfile.txt for lines containing “kamatera” (in any case), and:
- -i: Makes the search case-insensitive
- -A 4: Prints the matching line and 4 lines after it
Command:
grep -i -A 4 "kamatera" myfile.txt
- This command searches for the word “kamatera” (in any case) in myfile.txt and shows:
- The matching line
- 4 lines before and 4 lines after the match
Command:
grep -i -C 4 "kamatera" myfile.txt
And that’s it! We’ve reached the end of the list of Grep commands that you can use on a Linux server. Happy searching!