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

cd home/dir

grep -r -l -i "1896\.345" .
|improve this answer|||||
  • 2
    A dot instead of the asterisk would avoid any accidental shell expansion. – Thor Sep 11 '12 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 '12 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 '12 at 14:01

Use find and xargs:

find /home/dir -type f -print0 | xargs -0 grep -l 1896.345
|improve this answer|||||
  • 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 '12 at 13:30
  • You're right, I misread. Should work now. – Thor Sep 11 '12 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 '12 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 '12 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.