Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use command grep and locate

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)

like image 542
Jhonathan Avatar asked Sep 10 '12 15:09

Jhonathan


People also ask

What are grep find and locate in Linux?

There are several commands on Linux systems that allow you to search for files, with find and locate being the most used ones while the grep command is a search utility used primarily to return lines from a file, or files, that match a particular search term.

How do you grep and find?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

What is the difference between grep and locate?

The main difference between grep and find command in UNIX is that the grep is a command that helps to search content and display them according to the user-specified regular expression while the find command helps to search and locate files according to the given criteria.

How do I use grep to search for a word in a directory?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename. In the example below, we also added the -w operator to show whole words, but the output form is the same.

How do I use grep in Linux?

Grep is a powerful Linux command that lets you search for strings and expressions, across files and directories. It can be used to match patterns in log files, filter out useful information from the output of other commands, and perform recursive searches on nested directories.

How do I search a directory in grep?

By default, grep cannot search directories. If you try doing so, you'll get an error ("Is a directory"). With option -R, searching files within directories and subdirectories becomes possible.

How do I Grep a specific line in a string?

To Show Lines That Exactly Match a Search String The grep command prints entire lines when it finds a match in a file. To print only those lines that completely match the search string, add the -x option. grep -x “phoenix number3” *

How do I grep all subdirectories in Linux?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename. In the example below, we also added the -w operator to show whole words, but the output form is the same.


2 Answers

Instead of a pipe, use command replacement:

grep user `locate my.cnf`
like image 157
matt b Avatar answered Oct 10 '22 05:10

matt b


Try:

locate my.cnf | xargs grep user
like image 26
cha0site Avatar answered Oct 10 '22 06:10

cha0site