Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to use the grep facility on the whole server.

I tried the following:

grep -r 'MyString' / 
grep -r 'MyString' /*

However, none of these seem to work. Any suggestions?

share|improve this question
    
What are you looking for? File or folder names? Contents of (text) files? –  Bart Kiers Jan 5 '10 at 15:46
    
Define "don't work". They should certainly work. –  alvherre Jan 5 '10 at 15:48
add comment

migrated from stackoverflow.com Jan 5 '10 at 15:48

This question came from our site for professional and enthusiast programmers.

6 Answers

up vote 1 down vote accepted
sudo find / -type f -print0 |xargs -0 grep -l 'MyString'
share|improve this answer
add comment
cd /
find . | xargs grep MyString

If you're looking for particular file types:

find . -name *.java | xargs grep Integer
share|improve this answer
add comment
sudo find / -type f -exec grep "Strring" {} \;

Are you SURE you want to do this though? This will traverse all filesystems (local or not) and may very well max out the CPU on your server.

share|improve this answer
1  
Throw sudo in there so you can read all files and you'll have a winner! –  Carl Smotricz Jan 5 '10 at 15:46
    
Thanks carl, will do! –  ennuikiller Jan 5 '10 at 15:47
add comment

I second nont's approach.

But if your not root you'll get a whole bunch of permission denied messages, redirect those to /dev/null. Then you'll get what you want back.

 find . / 2>/dev/null | xargs grep MyString 2>/dev/null
share|improve this answer
add comment
cd /
grep -R 'your string' *
share|improve this answer
add comment

Install ack, which is packaged as ack-grep in the Ubuntu repositories. It is like a supercharged combination of find and grep that does exactly what you want without messing around with combinations of commands.

$ sudo ack-grep -a 'MyString' /

The -a option is used to override the default filtering behaviour of ack which limits the search to filetypes which it knows about. It will still exclude certain files and directories though, such as backup files that end in "~" or source control directories like ".svn". You can override this behaviour to search absolutely everything by using the -u option.

To limit the search to particular file types:

$ sudo ack-grep -aG '.*txt' MyString

noting that the argument to -G is a regex, not a glob.

Or for file types which ack knows about already, such as C++ files:

$ ack-grep --cpp 'SomeFunctionName'
share|improve this answer
add comment

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.