3

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
  • What are you looking for? File or folder names? Contents of (text) files?
    – user12915
    Jan 5, 2010 at 15:46
  • Define "don't work". They should certainly work.
    – alvherre
    Jan 5, 2010 at 15:48

6 Answers 6

1
sudo find / -type f -print0 |xargs -0 grep -l 'MyString'
5
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
  • 1
    Throw sudo in there so you can read all files and you'll have a winner! Jan 5, 2010 at 15:46
1
cd /
grep -R 'your string' *
0
cd /
find . | xargs grep MyString

If you're looking for particular file types:

find . -name *.java | xargs grep Integer
0

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
0

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'

You must log in to answer this question.

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