<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>State Of Flux &#187; grep</title>
	<atom:link href="http://stateofflux.com/tag/grep/feed/" rel="self" type="application/rss+xml" />
	<link>http://stateofflux.com</link>
	<description>always changing</description>
	<lastBuildDate>Fri, 29 Jan 2010 03:29:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A fast grep for Rails</title>
		<link>http://stateofflux.com/2007/06/17/a-fast-grep-for-rails/</link>
		<comments>http://stateofflux.com/2007/06/17/a-fast-grep-for-rails/#comments</comments>
		<pubDate>Sun, 17 Jun 2007 12:01:00 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://markmansour.wordpress.com//2008/04/02/a-fast-grep-for-rails</guid>
		<description><![CDATA[I often want to grep for a word recursively within a directory. That is easy! But quite often I know there are a bunch of directories that I can ignore. For instance, in my rails apps I don&#8217;t want to hear about subversion directories and I don&#8217;t want to search the vendor tree. I also [...]]]></description>
			<content:encoded><![CDATA[<p>I often want to grep for a word recursively within a directory.  That is easy!  But quite often I know there are a bunch of directories that I can ignore.  For instance, in my rails apps I don&#8217;t want to hear about subversion directories and I don&#8217;t want to search the vendor tree.</p>
<p>I also don&#8217;t want to look at files that are really big &#8211; they are usually log files or binaries of some sort that I just don&#8217;t care about.  So I put together the monster find and grep string that works for me.</p>
<pre lang="bash">
find . -path '*/.svn' -prune \
    -o -path '*/vendor' -prune \
    -o -path '*/log' -o -path '*~' -prune \
    -o \( -size -100000c -type f \) \
    -print0 | xargs -0 grep -ne "SEARCH"
</pre>
<p>Let&#8217;s break it down.</p>
<pre lang="bash">
find .
</pre>
<p>start searching for files recurively, starting from the current directory</p>
<pre lang="bash">
-path '*/.svn' -prune
</pre>
<p>ignore .svn directories</p>
<pre lang="bash">
-o -path '*/vendor'
</pre>
<p>OR ignore the vendor directory too.</p>
<pre lang="bash">
-o -path '*~'
</pre>
<p> OR ignore all the emacs backup files</p>
<pre lang="bash">
-o -path '*/log' -prune
</pre>
<p> ignore the log directory</p>
<pre lang="bash">
-o \( -size -100000c -type f \)
</pre>
<p> OR make sure the size is less than 100,000 characters and the type of thing is a file (not a pipe or a director or socket) &#8211; this usually gets rid of all non source files</p>
<pre lang="bash">
-print0
</pre>
<p>tells find to terminate the line with a 0.  Which sounds really dull, but you can&#8217;t pipe the data (get the output from &#8216;find&#8217; to &#8216;grep&#8217;) if there is a space in the filename.</p>
<pre lang="bash">
|
</pre>
<p>push the data from the command on the left to the command on the right</p>
<pre lang="bash">
xargs -0
</pre>
<p>take the input from the fine command, terminated with a 0 (the print0 from find) and send it to grep.</p>
<pre lang="bash">
grep -ne
</pre>
<p>search each file with a regular expression and when a match is found print the line number with the filename.</p>
<p>The easiest way to use this is to put it into a shell file &#8211; I&#8217;ve put it into s.sh.</p>
<p>My s.sh file looks like this</p>
<pre lang="bash">
#!/bin/sh

if  [ ${#1} -gt 0  ]
then
    find . -path '*/.svn' -prune -o -path '*/vendor' -prune -o -path '*/log' -o -path '*~' -prune -o \( -size -100000c -type f \) -print0 | xargs -0 grep -ne  $1
else
    echo "Not enough parameters.  Usage: ./$0 searchstring"
fi
</pre>
<p>Then I make it executable by</p>
<pre lang="bash">
chmod 755 s.sh
</pre>
<p>If you want to find all of the before filters you would do this</p>
<p>s.sh &#8220;before_filter&#8221;</p>
<p>and enjoy you super fast find command.</p>
]]></content:encoded>
			<wfw:commentRss>http://stateofflux.com/2007/06/17/a-fast-grep-for-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

