How do you grep and only return the matching line? i.e. The path/filename is omitted from the results.
In this case I want to look in all .bar files in the current directory, searching for the term FOO
find . -name '*.bar' -exec grep -Hn FOO {} \;
The quiet option ( -q ), causes grep to run silently and not generate any output. Instead, it runs the command and returns an exit status based on success or failure. The return status is 0 for success and nonzero for failure.
To exclude a filetype, you use rg --type-not cpp , to search only for a filetype you use rg --type cpp .
One such option is -l or --files-with-matches . When -l or --files-with-matches is enabled, only the names of files containing selected lines are written to standard output. The path names are listed once per file searched.
Search All Files in Directory To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command. The output shows the name of the file with nix and returns the entire line.
No need to find
. If you are just looking for a pattern within a specific directory, this should suffice:
grep -hn FOO /your/path/*.bar
Where -h
is the parameter to hide the filename, as from man grep
:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
Note that you were using
-H, --with-filename
Print the file name for each match. This is the default when there is more than one file to search.
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