grep — Case Insensitive Search

Searching for a pattern in files using grep is case-sensitive by default. Case-sensitive means that grep looks for an exact match of your pattern in the related file. This may not surface all search value matches because of different writing styles for a given text.

This tutorial shows you how to search a pattern case-insensitive in a file to see all matches, regardless the pattern’s writing style.

Ubuntu/Debian Series Overview

Case-Insensitive Search with grep

grep has a --ignore-case flag to ignore the casing when searching for a pattern in a file. This flag tells grep to treat all letters in the pattern and the text in the source file the same, no matter the casing.

You can use the --ignore-case flag or its shorthand -i for case-insensitive searching:

grep --ignore-case "pattern" storage/logs/app.log

# use the "-i" shorthand for "--ignore-case"
grep -i "pattern" storage/logs/app.log  

Examples

Let’s have a look at an example output of grep when searching for a string with the case-insensitive flag:

$ grep -i "request" storage/logs/app.log
INFO: ======= REQUEST FINISHED SUCCESSFUL ======> {"took":"0.052s","status_code":200}  

Here’s the related grep search without the case-insensitive flag. It won’t find a match because the search value request isn’t part of the app.log file:

$ grep -i "request" storage/logs/app.log

# empty output, nothing found

That’s it!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.