1

I am trying to locate a file that (should) contains the string "1896.345", somewhere under /home/dir/.

Any ideas of how I can do it? Thanks

0

2 Answers 2

5
cd home/dir

grep -r -l -i "1896\.345" .
3
  • 2
    A dot instead of the asterisk would avoid any accidental shell expansion.
    – Thor
    Sep 11, 2012 at 13:35
  • 2
    I have added an escape backslash to your answer, since otherwise strings like "1896-345" would also be matched.
    – January
    Sep 11, 2012 at 13:54
  • 1
    This will print the actual match, what the OP asks for is the name of the file, so grep -rl would do.
    – Thor
    Sep 11, 2012 at 14:01
0

Use find and xargs:

find /home/dir -type f -print0 | xargs -0 grep -l 1896.345
4
  • I assumed the question meant searching for text within the file, in which case you can use "find /home/dir -type f -exec grep 1896.345 {} \;" - but be warned that this may take a lot of time if there are many files, and will only work if the text is in a plain text file (not eg, LibreOffice document).
    – chronitis
    Sep 11, 2012 at 13:30
  • You're right, I misread. Should work now.
    – Thor
    Sep 11, 2012 at 13:31
  • Yeah, but why spawn a separate grep instance for every file that you want to search, when grep -r does the job? That solution makes sense only if you want to limit your search by some other limits (like file creation date, file type, permissions etc.)
    – January
    Sep 11, 2012 at 13:52
  • xargs passes as many files as fit into MAXARG to grep. But you're about it being redundant when grep -r pattern . will do.
    – Thor
    Sep 11, 2012 at 13:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .