How to Make Grep Case Insensitive
Grep is a command used to find words in a string or file. By default, grep is case sensitive. That means all of these search terms would be treated differently:
- “COMMAND LINE”
- “Command line”
- “CoMMand LiNe”
To clarify, if my file contained the string “command line”, none of the above searches would return anything.
Grep Ignore Case
You can add an [.inline-code]-i[.inline-code] or [.inline-code]–ignore-case[.inline-code] flag to make grep case sensitive:
[.inline-code]grep -i “command line” textfile.txt[.inline-code]
[.inline-code]grep –ignore-case “command line” textfile.txt[.inline-code]
Running Through an Example
1. Run the following in your terminal:
This command will create a file called [.inline-code]grepFacts.txt[.inline-code], put the 6 facts of grep that we’ve written out for you into that file, and save it in your current directory.
See command and expected output here
2. [Feel Free To Skip This Step] If you’re curious or want to verify that this command is actually saved in your file, run [.inline-code]cat grepFacts.txt[.inline-code]. This command will output all the contents of the file [.inline-code]grepFacts.txt[.inline-code].
See command and expected output here
3. Run [.inline-code]grep “grep” grepFacts.txt[.inline-code].
As you can see, this command only matches with the lowercase version of “grep”.
See command and expected output here
4. Now, run [.inline-code]grep -i “grep” grepFacts.txt[.inline-code].
As you can see, this command matches with all instances of “grep”, regardless of case.
See command and expected output here
Conclusion
To recap, the [.inline-code]grep[.inline-code] command allows you to search for a pattern inside of files, and is case sensitive by default. To make grep case insensitive, all you have to do is add an [.inline-code]-i[.inline-code] or [.inline-code]—ignore-case flag[.inline-code].
As always, you can type [.inline-code]man grep[.inline-code] into your command line to get the official documentation for grep and all its flags and parameters. If you want to learn more, check out this page for more information on the [.inline-code]grep[.inline-code] command.