Grep on the whole server in Ubuntu

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?

2

6 Answers

sudo find / -type f -print0 |xargs -0 grep -l 'MyString'
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.

1
cd /
grep -R 'your string' *
cd /
find . | xargs grep MyString

If you're looking for particular file types:

find . -name *.java | xargs grep Integer

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

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'

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like