-w in grep command in linux
Just understand :)
The -w
option in the grep
command in Linux stands for "whole word" and it tells grep
to only match lines that contain the entire word that you're searching for, instead of matching lines that contain the word as a substring of a larger word.
For example, suppose you have a file called myfile.txt
with the following contents:
apple
apples
pineapple
If you run the command
grep "apple" myfile.txt
, it will match all three lines because all of them contain the substring "apple". However, if you use the-w
option like this:grep -w "apple" myfile.txt
, it will only match the first line that contains the whole word "apple" and not the other lines that contain "apples" or "pineapple".The
-w
option is useful when you want to search for a specific word and not its substrings or partial matches. It can be combined with othergrep
options to perform more complex searches, such as searching for whole words in specific files or directories, or searching for whole words in the output of other commands.