Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 5 down vote accepted
cd home/dir

grep -r -l -i "1896\.345" .
share|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
share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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