Christian Biggins Design, Technology, SEO, General Ramblings. Something for everybody.

24Nov/080

The common grep –exclude problem

grep is a file content searching tool for Linux. It is loved by programmers for its ease of use and swift search results (with customisable output). For example, if you forgot where you left your debug code, you can quickly display a list of the files that contain the code like so;

grep -ril 'var_dump' *

But, the problem with grep is that it is not possible to limit the subdirectories it searches within based on a pattern. A lot of people misunderstand the "-exclude" option, thinking that they can stop grep from returning results from SVN directories using it like so;

grep -ril --exclude=".svn/*" 'var_dump' *

Little do they realise that exclude is for file names only and grep has no functionality for restricting search based on directory names. grep can invert the found rows and only show the non-matching lines using the '-v' parameter and can remove the .svn directories but it is very slow;

grep -ril "'var_dump' * | grep -v svn

So, in comes ack. ack is much faster than grep (it uses perl regular expressions) and searches recursively and ignores revision control subdirectories (.svn) by default. Also, it displays the found code in a much easier to read format (if you opt to display the lines as opposed to just the file names);

ack To make things easier, ack is available in a lot of repo's for different Linux distributions.  I was able to get ack this easily;

sudo apt-get install ack

Done. Its also available as a perl script incase you cannot install it due to dictator sys admins (you know the type.).

If you code and use grep for your line-level searching, do yourself a favour and get ack.

No related posts.

blog comments powered by Disqus