Skip to main content
Added explanations and proper formatting.
Source Link
Caleb
  • 71.9k
  • 19
  • 203
  • 232

If your search results are sure to return paths with no spaces, you could use xargs like this:

locate my.cnf | xargs grep user  (no search paths with blanks)

However you should get in the habit of protecting yourself to handle the case where a path or filename might contain a space by telling xargs to use null as a separator and telling locate (or whatever program you are using to return strings) to also send that as the separator like this:

locate -0 my.cnf | xargs -0 grep user (search paths with blanks)

Example path with blanks:  "/name with space/my.cnf"

This would work even if your path included blanks like /name with space/my.cnf.

locate my.cnf | xargs grep user  (no search paths with blanks)

locate -0 my.cnf | xargs -0 grep user (search paths with blanks)

Example path with blanks:  "/name with space/my.cnf"

If your search results are sure to return paths with no spaces, you could use xargs like this:

locate my.cnf | xargs grep user

However you should get in the habit of protecting yourself to handle the case where a path or filename might contain a space by telling xargs to use null as a separator and telling locate (or whatever program you are using to return strings) to also send that as the separator like this:

locate -0 my.cnf | xargs -0 grep user

This would work even if your path included blanks like /name with space/my.cnf.

added 171 characters in body
Source Link
Jhonathan
  • 3.8k
  • 4
  • 27
  • 23
locate my.cnf | xargs grep user  (no search paths with blanks)

locate -0 my.cnf | xargs -0 grep user (search paths with blanks)

Example path with blanks:  "/name with space/my.cnf"
locate my.cnf | xargs grep user
locate my.cnf | xargs grep user  (no search paths with blanks)

locate -0 my.cnf | xargs -0 grep user (search paths with blanks)

Example path with blanks:  "/name with space/my.cnf"
Source Link
Jhonathan
  • 3.8k
  • 4
  • 27
  • 23

locate my.cnf | xargs grep user