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
- Fix “sudo command not found”
- Install a Specific Version with apt-get on Ubuntu/Debian
- Fix Ubuntu/Debian apt-get “KEYEXPIRED: The following signatures were invalid”
- How to Test a Cron Job
- How to Unzip Into a Folder
- How to Show Your Elasticsearch Version on Ubuntu/Debian
- Use “which” in Linux to find the Location of an Exetable
- Sort “ls” by Last Changed Date
- How to Shutdown a Machine
- Show Hidden Files and Folders with `ls`
- Case Insensitive Search
- Search Recursive in Subdirectories
- Search all Files in a Directory (Non-Recursively)
- Search in Multiple Log Files
- Search in Selected Files
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!