Basic Usage
The basic syntax of grep is:/etc/os-release file:
Case-Insensitive Search
To perform a search regardless of case sensitivity, use the-i option. This option ensures that grep matches “CentOS”, “centos”, or even “CENTOS”, as shown below:
Recursive Search
If you need to search through an entire directory and its subdirectories, leverage the-r option (recursive search). For instance, to search for “CentOS” in all files under the /etc/ directory, use:
If you encounter “Permission denied” errors, it is likely because your current user lacks the necessary read permissions for some system files.
-ir options:
sudo to gain the appropriate permissions:
Inverting the Search
Sometimes you may want to display lines that do not match a specific pattern. Use the-v option, which inverts the match. The following example shows how to list lines in /etc/os-release that do not contain the text “centos” (case insensitive):
Matching Whole Words
If you need to search for an exact word and avoid partial matches, use the-w option. For example, to search for only the whole word “red” in a case-insensitive manner:
Extracting Only the Matched Text
By default, grep prints the entire line where the match is found. If you want to extract only the matching segment, use the-o option: