How I can make the grep command locate certain words in the files specified by the routes found by the locate command?
locate my.cnf | grep user
(I want that grep command search the word "user" on the files found for locate command)
How I can make the grep command locate certain words in the files specified by the routes found by the locate command?
locate my.cnf | grep user
(I want that grep command search the word "user" on the files found for locate command)
Instead of a pipe, use command replacement:
grep user `locate my.cnf`
--color=always
. –
Litigant Try:
locate my.cnf | xargs grep user
xargs
. –
Trichromatism Instead of a pipe, use command replacement:
grep user `locate my.cnf`
xargs
approach below because it maintains grep's highlighting –
Emotionality --color=always
. –
Litigant In order to play nice with situations when locate results have spaces in names, you could do this
locate -0 my.cnf | xargs -n1 -0 grep user
grep
automatically outputs all of the matching lines (your file likely has multiple lines that match). If you wish to show line numbers in front of them you could use -n
switch for grep. Otherwise, you could have it output only the first match for every file with -m 1
or only the name of the file that matches with -l
. –
Dermato locate -0
is indeed necessary if file paths contain spaces. However, in bash, to see the full path of the file, I have to add -H
to grep, like so: locate -0 *.tex | xargs -n1 -0 grep -H user
–
Nowak Probably grep user $(locate my.cnf)
is what you're looking for, if I understand your question correctly.
locate
command returns too much data. Command line size is not infinite. –
Litigant © 2022 - 2024 — McMap. All rights reserved.
xargs
approach below because it maintains grep's highlighting – Emotionality