grep is a command frequently used for text searches, but if you don't understand how it handles uppercase and lowercase letters, you may not get the results you expect.
Beginners in particular often get stuck on cases where the reason no search results appeared turned out to be a difference in letter case.
This article explains everything from the basics to more advanced usage in detail
Reference: GNU grep
How to Make grep Case Insensitive
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
BANANA
banana
EOF
Command
grep "Apple" input.txt
Output
Apple
Command
grep -i "Apple" input.txt
Output
Apple
apple
APPLE
How It Works
| Item | Description |
|---|---|
| grep | By default, searches while distinguishing between uppercase and lowercase letters. |
| grep -i | Specifying -i (--ignore-case) makes the search case insensitive. |
| Use case | Useful when you want to search without worrying about letter case, such as with user input or log analysis. |
Explanation
By default, grep distinguishes between uppercase and lowercase letters.
Simply adding the -i option lets you search without distinguishing case, enabling more flexible string searches.
Basics of the grep Case Insensitive Option (-i)
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
Cherry
EOF
Command
grep -i "apple" input.txt
Output
Apple
apple
APPLE
Command
grep -i "banana" input.txt
Output
Banana
banana
How It Works
| Item | Description |
|---|---|
| grep | A command that searches for lines containing a specified pattern |
| -i | An option that searches without distinguishing uppercase and lowercase letters |
| "apple" | The string being searched for |
| input.txt | The file being searched |
Explanation
Normally grep distinguishes between uppercase and lowercase letters when searching, but adding the -i option lets you search without that distinction.
This is useful when you want to search for variations like Apple, apple, and APPLE all together.
grep Case Insensitive vs Case Sensitive Options (--ignore-case)
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
BANANA
EOF
Command
grep -i "apple" input.txt
Output
Apple
apple
APPLE
Command
grep --ignore-case "banana" input.txt
Output
Banana
banana
BANANA
How It Works
| Item | Description |
|---|---|
| -i | The short form of --ignore-case. Searches without distinguishing uppercase and lowercase letters. |
| --ignore-case | The long form of -i. The functionality is exactly the same; used when readability is a priority. |
| Difference | There is no difference in function or behavior; only the way it is written (short form vs. long option) differs. |
Explanation
Since -i and --ignore-case have the same function, you can choose whichever suits your preference or readability needs.
Use -i for brevity, or --ignore-case when you want to make the meaning clear, such as in scripts.
How to Perform a grep Case Insensitive Word Search
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
Orange
EOF
Command
grep -i "apple" input.txt
Output
Apple
apple
APPLE
How It Works
| Item | Description |
|---|---|
| grep | A command that searches text for lines matching a condition |
| -i | An option that searches without distinguishing uppercase and lowercase letters (ignore case) |
| "apple" | The word being searched for |
| input.txt | The file being searched |
Explanation
Using grep's -i option lets you search for strings while ignoring differences in uppercase and lowercase letters.
This is useful for searching data that has variations such as Apple, apple, and APPLE.
How to Perform an Exact Match grep Case Insensitive Search
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
EOF
Command
grep -ix "apple" input.txt
Output
Apple
apple
APPLE
How It Works
| Option | Role |
|---|---|
| grep | A command that searches text |
| -i | Searches without distinguishing uppercase and lowercase letters |
| -x | Targets only lines whose entire content exactly matches the search string |
| "apple" | The string being searched for |
| input.txt | The file being searched |
Explanation
To perform an exact match search that ignores uppercase and lowercase letters, combine -i and -x.
This way, Apple and APPLE will match, but partial matches like apple123 or myapple will not be included in the results.
grep Case Insensitive Regular Expression Search
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
Orange
EOF
Command
grep -Ei 'apple|banana' input.txt
Output
Apple
apple
APPLE
Banana
banana
Command
grep -i '^apple$' input.txt
Output
Apple
apple
APPLE
How It Works
| Item | Description |
|---|---|
| grep | A command that searches a file for lines matching a condition |
| -i | Searches without distinguishing uppercase and lowercase letters |
| -E | Enables extended regular expressions, allowing OR searches with | |
| apple|banana | A regular expression that matches either apple or banana |
| ^apple$ | Matches only when the entire line is apple (case ignored due to -i) |
Explanation
Using grep -i lets you search for strings without distinguishing uppercase and lowercase letters.
Combining it with -E allows you to concisely write extended regular expressions, such as OR searches with |.
grep Case Insensitive Multiple Pattern Search
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
Cherry
CHERRY
Grape
EOF
Command
grep -Ei 'apple|cherry' input.txt
Output
Apple
apple
APPLE
Cherry
CHERRY
How It Works
| Item | Description |
|---|---|
| grep | A command that searches text for lines matching a condition |
| -E | Enables extended regular expressions, allowing OR searches like pattern1|pattern2 |
| -i | Searches without distinguishing uppercase and lowercase letters |
| apple|cherry | Searches for lines matching apple or cherry |
| input.txt | The file being searched |
Explanation
Specifying -i lets you search while ignoring differences in case, such as Apple, APPLE, and apple.
Combining -E with | lets you concisely specify multiple search patterns.
grep Case Insensitive Recursive Search
Create the File
mkdir -p docs logs src
Create the File
cat << 'EOF' > docs/README.md
This is Sample Text
EOF
Create the File
cat << 'EOF' > docs/guide.txt
sample text
EOF
Create the File
cat << 'EOF' > logs/app.log
ERROR: Connection Failed
EOF
Create the File
cat << 'EOF' > logs/system.log
error: disk full
EOF
Create the File
cat << 'EOF' > src/main.py
Print("Hello World")
EOF
Create the File
cat << 'EOF' > src/test.py
print("hello world")
EOF
Command
grep -ri "error" .
Output
./logs/system.log:error: disk full
./logs/app.log:ERROR: Connection Failed
How It Works
| Item | Description |
|---|---|
| grep | A command that searches text |
| -r | Searches recursively through the specified directory |
| -i | Searches without distinguishing uppercase and lowercase letters |
| "error" | The string being searched for. Treats ERROR, Error, and error as the same |
| . | Targets everything under the current directory |
Explanation
Using grep -ri lets you search for a string across all files in a directory tree without distinguishing uppercase and lowercase letters.
This is often used to efficiently find specific keywords across an entire set of logs or source code.
How to Limit grep Case Insensitive Search to Specific Files
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
Orange
EOF
Command
grep -i "apple" input.txt
Output
Apple
apple
APPLE
Command
grep -i "banana" input.txt
Output
Banana
banana
How It Works
| Item | Description |
|---|---|
| grep | A command that searches for a specified string within a file. |
| -i | Searches without distinguishing uppercase and lowercase letters (ignore case). |
| "apple" | Specifies the string to search for. |
| input.txt | Specifies the file to search. Placing it after the options limits the search target. |
Explanation
Using grep -i lets you search for strings without distinguishing uppercase and lowercase letters. By specifying the file name at the end, you can limit the search to a specific file.
Displaying grep Case Insensitive Search Results with Line Numbers
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
Cherry
EOF
Command
grep -in "apple" input.txt
Output
1:Apple
2:apple
3:APPLE
How It Works
| Item | Description |
|---|---|
| grep | A command that searches for lines containing a specified string |
| -i | Searches without distinguishing uppercase and lowercase letters (ignore case) |
| -n | Displays the line numbers of matching lines |
| "apple" | The string being searched for |
| input.txt | The file being searched |
Explanation
Adding the -i option lets you match lines even when the case differs, as with Apple, apple, and APPLE.
Combining it with -n lets you also see the line numbers of the matches at the same time.
Showing Lines Before and After grep Case Insensitive Matches
Create the File
cat << 'EOF' > input.txt
Linux
linux
Ubuntu
LINUX
Debian
CentOS
LiNuX
Fedora
EOF
Command
grep -i -C 1 "grep" input.txt
Output
no output
Command
grep -i -C 1 "linux" input.txt
Output
Linux
linux
Ubuntu
LINUX
Debian
CentOS
LiNuX
Fedora
How It Works
| Option | Description |
|---|---|
| grep | A command that searches for a specified string |
| -i | Searches without distinguishing uppercase and lowercase letters |
| -C 1 | Also displays 1 line before and after each matching line |
| "linux" | The string being searched for |
| input.txt | The file being searched |
Explanation
Specifying -i lets you search for Linux, LINUX, LiNuX, and other case variations as the same string.
Combining it with -C 1 also displays 1 line before and after each matching line, letting you check the context of the results.
Using grep Case Insensitive Results with Pipes
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
banana
Cherry
EOF
Command
grep -i "apple" input.txt
Output
Apple
apple
APPLE
Command
grep -i "apple" input.txt | wc -l
Output
3
Command
grep -i "banana" input.txt | sort
Output
Banana
banana
How It Works
| Item | Description |
|---|---|
| grep | A command that searches text |
| -i | An option that searches without distinguishing uppercase and lowercase letters |
| | | Passes the output of the previous command to the next command (pipe) |
| wc -l | Counts the number of lines received through the pipe |
| sort | Sorts the search results received through the pipe |
Explanation
Using grep -i lets you search without worrying about uppercase or lowercase letters.
Combining it with a pipe (|) makes it easy to apply further processing, such as counting or sorting the results.
Combining grep Case Insensitive Search with the find Command
Create the File
cat << 'EOF' > input.txt
Hello World
HELLO Linux
hello grep
Sample Text
HELLO FIND
Goodbye
EOF
Command
find . -name "input.txt" -exec grep -i "hello" {} \;
Output
Hello World
HELLO Linux
hello grep
HELLO FIND
How It Works
| Mechanism | Description |
|---|---|
| find . | Starts the search from the current directory |
| -name "input.txt" | Searches for a file named input.txt |
| -exec | Runs a command against each file that is found |
| grep -i "hello" | Searches for hello without distinguishing uppercase and lowercase letters |
| {} | Is replaced with the file name found by find |
| \; | Marks the end of -exec |
Explanation
Using grep's -i option lets you search for a string without distinguishing uppercase and lowercase letters.
Combining it with the find command lets you efficiently search for matching strings across multiple files.
Causes and Fixes When grep Case Insensitive Search Doesn’t Work
Create the File
cat << 'EOF' > input.txt
Apple
apple
APPLE
Banana
BANANA
orange
Orange
EOF
Command
grep "apple" input.txt
Output
apple
Command
grep -i "apple" input.txt
Output
Apple
apple
APPLE
Command
grep --ignore-case "banana" input.txt
Output
Banana
BANANA
How It Works
| Item | Description |
|---|---|
| Cause | By default, grep searches while distinguishing between uppercase and lowercase letters. |
| Fix | Specify the -i option when searching. |
| Long option | --ignore-case behaves the same way. |
| Effect | Allows matching lines to be found while ignoring differences in uppercase and lowercase letters. |
Explanation
Most of the time, the reason a grep case insensitive search doesn't work is simply that the -i option was not included.
Specifying -i or --ignore-case lets you search flexibly without worrying about uppercase and lowercase letters.
Commonly Used Options for grep Case Insensitive Search
Create the File
cat << 'EOF' > input.txt
Hello World
hello world
HELLO WORLD
Linux
linux
LiNuX
Grep
grep
EOF
Command
grep -i "hello" input.txt
Output
Hello World
hello world
HELLO WORLD
Command
grep -i "linux" input.txt
Output
Linux
linux
LiNuX
Command
grep -in "grep" input.txt
Output
7:Grep
8:grep
Command
grep -iv "hello" input.txt
Output
Linux
linux
LiNuX
Grep
grep
How It Works
| Option | Description | Behavior |
|---|---|---|
| -i | Does not distinguish uppercase and lowercase letters | Treats Hello, hello, and HELLO as the same string when searching |
| -in | Case insensitive search that also shows line numbers | Adds a line number to the beginning of each matching line |
| -iv | Case insensitive search that shows non matching lines | Outputs only lines that do not contain the search string |
Explanation
The -i option is the one most commonly used for grep case insensitive searches.
Combining it with -n or -v also lets you efficiently check results or exclude matches.
grep Case Insensitive Search Key Points Summary
By default, grep distinguishes between uppercase and lowercase letters when searching.
To search without distinguishing them, use -i or --ignore-case.
Understanding the role of these basic options is the shortcut to using grep efficiently.

