Installing and Customizing Skype (and other QT apps) on 64 Bit Ubuntu Compiz and Emerald problem after Ubuntu Hardy Heron Upgrade
May 13
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

There are times - many in fact - when I need to find a file, or multiple files containing a particular string. For example, I’m developing a web application, and need to find out which pages call a particular class method, say, all the files which call the getTopTenCDs() method - I can do this by opening a Terminal, and running grep (Global Regular Expression Print), with a couple of options:

grep -r -n 'getTopTenCDs()' /var/www/cdcollection/*

This will return a list of files, and the lines within those files, and the line numbers containing the string ‘getTopTenCDs()’ in the directory /var/www/cdcollection/. The -n option tells grep to output the line numbers, the -r option tells grep to search recursively through the sub-directories too, and * tells it to look in all files - which can be changed to *.php etc: /var/www/cdcollection/*.php

So, the output would be something along the lines of:

/var/www/cdcollection/viewcd.php:192:   $topTen = $cd->getTopTenCDs();
/var/www/cdcollection/sidebar.php:215:   $topTen = $cd->getTopTenCDs();

If I just want a list of files without the lines containing the search string, I can use the -l option:

grep -r -l 'getTopTenCDs()' /var/www/cdcollection/*

which would simply return:

/var/www/cdcollection/viewcd.php
/var/www/cdcollection/sidebar.php

As the name implies, grep uses Regular Expressions, and therefore the search string can contain the usual RegEx operatiors: . ? * + {n} etc. RegEx is out of scope for this post I’m affraid - there are dozens of great tutorials available on RegEx, and applying RegEx to grep.

One final point about grep: if the search string begins with a hyphen (minus sign, or whatever you want to call “-”), the search string needs to be preceded with the -e option:

grep -r -e '-starts with a hyphen' /var/www/cdcollection/*

so that grep doesn’t mistake the search pattern for an option! Forget the -e option, and you’ll get:

grep: invalid option -- $
Usage: grep [OPTION]… PATTERN [FILE]…
Try `grep –help’ for more information.

Also, remember the ever useful pipe | and output to file > commands. If the list printed by grep is huge, you can simply pipe it to more:

grep -r 'getTopTenCDs()' /var/www/cdcollection/* | more

or, if you want to save it to a file to peruse later:

grep -r 'getTopTenCDs()' /var/www/cdcollection/* > ~/grep_output.txt

Always useful.

Finally, remember to take a look at the grep man page:

man grep

Which contains a world of useful information, including a wealth of options I haven’t covered.


60 GB Toshiba Notebook Laptop IDE Hard Drive 2.5 IN
US $28.00 (11 Bids)
End Date: Friday Sep-05-2008 16:26:13 PDT
Bid now | Add to watch list
1 DELL LATITUDE XP P4 2 M Wi Fi WIFI LAPTOP NOTEBOOK NR
US $200.00 (21 Bids)
End Date: Friday Sep-05-2008 16:33:00 PDT
Bid now | Add to watch list
HP Pavilion 3GB DV6629WM Laptop PC Notebook DV6000 Dual
US $385.00 (4 Bids)
End Date: Friday Sep-05-2008 17:29:10 PDT
Bid now | Add to watch list
PINK LAPTOP HANDBAG NOTEBOOK CASE TOTE BAG DELL HP IBM2
US $24.99 (1 Bid)
End Date: Friday Sep-05-2008 17:45:03 PDT
Bid now | Add to watch list
1 DELL LATITUDE XP P4 2 M Wi Fi WIFI LAPTOP NOTEBOOK NR
US $207.50 (31 Bids)
End Date: Friday Sep-05-2008 18:19:05 PDT
Bid now | Add to watch list
LIMITED EDITION ALIENWARE SUPERMAN LAPTOP NOTEBOOK
US $760.00 (21 Bids)
End Date: Friday Sep-05-2008 19:00:00 PDT
Bid now | Add to watch list


  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • StumbleUpon
  • Technorati
  • Slashdot

Related posts:

  1. Searching for files - beagle, Google Desktop, find / | grep

written by Hodge \\ tags: , , ,

Leave a Reply

Webloogle Blog Directory