This question is based on this answer.
Why do you get the same output from the both commands?
Command A
$sudo grep muel * /tmp
masi:muel
Command B
$sudo grep -H muel * /tmp
masi:muel
Rob's comment suggests me that Command A should not give me masi:
, but only muel
.
In short, what is the practical purpose of -H
?
cat command: It is used to create the file with content. touch command: It is used to create a file without any content. The file created using touch command is empty. This command can be used when the user doesn't have data to store at the time of file creation.
The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out). Options Description -c : This prints only a count of the lines that match a pattern -h : Display the matched lines, but do not display the filenames.
To search for a string in a file, run the command below Syntax $ grep "string" file name. OR $ filename grep "string" Example: $ grep "Linux" welcome.txt. Output As you can see, grep has not only searched and matched the string “Linux” but has also printed the lines in which the string appears.
Grep will list the filenames by default if more than one filename is given. The -H
option makes it do that even if only one filename is given. In both your examples, more than one filename is given.
Here's a better example:
$ grep Richie notes.txt
Richie wears glasses.
$ grep -H Richie notes.txt
notes.txt:Richie wears glasses.
It's more useful when you're giving it a wildcard for an unknown number of files, and you always want the filenames printed even if the wildcard only matches one file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With