<?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>Do Know Evil - A Blog by Tyler Mulligan &#187; search</title> <atom:link href="http://www.doknowevil.net/tag/search/feed/" rel="self" type="application/rss+xml" /><link>http://www.doknowevil.net</link> <description>Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like</description> <lastBuildDate>Sat, 16 Jul 2011 01:25:35 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.5</generator> <item><title>Finding files and strings using the terminal in Linux</title><link>http://www.doknowevil.net/2009/02/04/finding-files-and-strings-using-the-terminal-in-linux/</link> <comments>http://www.doknowevil.net/2009/02/04/finding-files-and-strings-using-the-terminal-in-linux/#comments</comments> <pubDate>Wed, 04 Feb 2009 22:06:43 +0000</pubDate> <dc:creator>Tyler Mulligan</dc:creator> <category><![CDATA[Bash]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[find]]></category> <category><![CDATA[grep]]></category> <category><![CDATA[locate]]></category> <category><![CDATA[search]]></category><guid isPermaLink="false">http://www.doknowevil.net/2009/02/04/finding-files-and-strings-using-the-terminal-in-linux/</guid> <description><![CDATA[My favorite thing about Linux is the terminal. I use it countless times a day to do all sorts of tasks, like managing game servers or writing scripts to do tedious tasks. One of the most popular things I do in the terminal is search for files or strings inside of files and today I&#8217;d]]></description> <content:encoded><![CDATA[<p>My favorite thing about Linux is the terminal.  I use it countless times a day to do all sorts of tasks, like <a href="http://github.com/z/nst/" title="Nexuiz Server Toolz" target="_blank">managing game servers</a> or <a href="http://pics.nexuizninjaz.com/files/nk1g9e0z5i1jxlgwiszw.png" title="revision map packages with bash">writing scripts to do tedious tasks</a>.  One of the most popular things I do in the terminal is search for files or strings inside of files and today I&#8217;d like to go over a few methods and tricks for doing this.  There are three tools that make this task amazingly easy but combining them is where we find the real power.  These three tools are locate, find and grep.  I will cover the basic use of these tools with some examples and tricks but I suggest you take a look at the man pages for the tools for addition information (i.e. man locate).</p><h3>locate</h3><p>Locate has got to be the most straight forward way to search for files.  It uses a database so it&#8217;s blazing fast when searching your entire computer, compared to the &#8216;find&#8217; we&#8217;ll cover later.  It&#8217;s not available by default on all Linux distributions (it is on Ubuntu) so you might have to install it.  Since I&#8217;ll be mentioning fstab later, I&#8217;ll give my examples with it.</p><p>Basic syntax is: locate [filename]</p><pre class="brush:bash">tyler@quadjutsu:~$ locate fstab
/etc/fstab
/etc/fstab.orig
/etc/fstab.pre-ntfs-config
/etc/fstab~
/usr/include/fstab.h
/usr/lib/udev/migrate-fstab-to-uuid.sh
/usr/share/apps/katepart/syntax/fstab.xml
/usr/share/doc/m4/examples/fstab.m4
/usr/share/doc/mount/examples/fstab
/usr/share/doc/util-linux/examples/fstab.example2
/usr/share/man/man5/fstab.5.gz
/usr/share/pysdm/fstab.py
/usr/share/pysdm/fstab.pyc</pre><p>This database will automatically update itself at various times but to force an update type the following command:</p><pre class="brush:bash">tyler@quadjutsu:~$ sudo updatedb</pre><p>Folder exclusions can be found (and edited) in /etc/updatedb.conf</p><p>By default (at least in ubuntu) /media (your mounted media) is not included in this database.  This means if you use extra drives you&#8217;ve added to your computer and you want them to be searchable through the locate tool, you&#8217;ll have to mount them to a directory like /mnt.</p><p>To mount a drive on boot, you&#8217;ll need to add a line like the following to your /etc/fstab.  The one below mounts an ntfs drive to /mnt/mydrive.  That folder must exist for the drive to be mounted.</p><pre class="brush:bash">/dev/sda1 /mnt/mydrive ntfs-3g defaults,locale=en_US.utf8 0 0</pre><p>I found the /dev/sda1 part by listing my harddrive partitions using the following command:</p><pre class="brush:bash">sudo fdisk -l</pre><h3>find</h3><p>Find is a little more cryptic than locate but it&#8217;s a very powerful tool that should be available on most Linux distributions if not all.</p><p>Basic syntax is: find [directory] -name &#8220;[string]&#8221; -print</p><p>Find will search recursively through the folder you&#8217;re calling it from.  The directory isn&#8217;t required but it will show you the full path.  A neat trick is to use $(pwd) which creates a string for the &#8220;present working directory&#8221;.  It&#8217;s also a good habit to use quotes around your name search because you can&#8217;t do regular expressions without it.  find does not do matching the same way locate does.  You&#8217;ll have to specific a wild card (*) for partial searches.</p><pre class="brush:bash">tyler@quadjutsu:~/Desktop$ find -name &quot;notes&quot; -print
tyler@quadjutsu:~/Desktop$ find -name &quot;notes*&quot; -print
./notes.txt~
./notes.txt
tyler@quadjutsu:~/Desktop$ find $(pwd) -name &quot;notes*&quot; -print
/home/tyler/Desktop/notes.txt~
/home/tyler/Desktop/notes.txt
tyler@quadjutsu:~/Desktop$ find -name &quot;no[a-z]*&quot; -print
./notes.txt~
./notes.txt</pre><p>My Buddy From Belgium, MrBougo has asked I make note that -iname makes it case insensitive.</p><h3>grep</h3><p>In the context of searching, grep is like a pocket knife.  It&#8217;s great for limiting returned results and searching through files, which are my two most common uses of it, though I&#8217;m sure there are a million others.  First I&#8217;ll cover the searching files for strings portion and in combined tools we&#8217;ll discuss how to refine search results.</p><p>In terms of searching strings in files, basic syntax is: grep &#8220;[string]&#8221; [filename]</p><p>grep is case sensitive but can be changed to insensitive with -i.  You can access extended regular expressions (which allow for such functions as use of the + sign to signify 1 or more characters) by calling &#8220;egrep&#8221;</p><pre class="brush:bash">tyler@quadjutsu:~$ grep &quot;tyler&quot; resume.txt
tyler@detrition.net
tyler@quadjutsu:~$ grep &quot;Tyler&quot; resume.txt
Tyler J. Mulligan
tyler@quadjutsu:~$ grep -i &quot;tyler&quot; resume.txt
Tyler J. Mulligan
tyler@detrition.net
tyler@quadjutsu:~$ grep -i &quot;tyl&quot; resume.txt
Tyler J. Mulligan
tyler@detrition.net
tyler@quadjutsu:~$ egrep -i &quot;tyler [a-z.]+&quot; resume.txt
Tyler J. Mulligan
tyler@quadjutsu:~$ egrep &quot;tyler [a-z.]+&quot; resume.txt</pre><p>MrBougo also mentioned that, grep returns a 1 exit status if it doesnt find, so grep &#8220;foo&#8221; &#038;&#038; grep &#8220;bar&#8221; will only grep for bar when foo is found.</p><h3>Combining the tools</h3><p>There are two very important techniques for linking together commands in the terminal.  The pipe | and &#038;&#038;.  The pipe passes the output of one command to the next, the &#038;&#038; runs a command after the one before is complete.</p><p>Starting off slow, we&#8217;ll refine our search of grep with another grep.  Note that you don&#8217;t have to use the quotes as I&#8217;ve shown below.</p><pre class="brush:bash">
tyler@quadjutsu:~$ grep -i &quot;nexuiz&quot; resume.txt
    * Web Developer and Interaction Designer for Nexuiz / Alientrap
    * Owner and Creator of Nexuiz Ninjaz
tyler@quadjutsu:~$ grep -i &quot;nexuiz&quot; resume.txt |grep -i ninjaz
    * Owner and Creator of Nexuiz Ninjaz</pre><p>Not that you would really want to do the follow commands but to emphasize how | and &#038;&#038; work, the following example shows how you&#8217;re running the command twice rather than refining a search:</p><pre class="brush:bash">
tyler@quadjutsu:~$ grep -i &quot;nexuiz&quot; resume.txt &amp;&amp; grep -i &quot;ninjaz&quot; resume.txt
    * Web Developer and Interaction Designer for Nexuiz / Alientrap
    * Owner and Creator of Nexuiz Ninjaz
    * Owner and Creator of Nexuiz Ninjaz
</pre><p>Recalling our first example with fstab, there was a lot of extra results we didn&#8217;t want.  We know know it was in the etc folder, so we can filter our results by that.</p><pre class="brush:bash">tyler@quadjutsu:~$ locate fstab |grep etc
/etc/fstab
/etc/fstab.orig
/etc/fstab.pre-ntfs-config
/etc/fstab~
</pre><p>Another way would be to EXCLUDE folders we don&#8217;t want with the -v flag.</p><pre class="brush:bash">tyler@quadjutsu:~$ locate fstab |grep -v usr
/etc/fstab
/etc/fstab.orig
/etc/fstab.pre-ntfs-config
/etc/fstab~</pre><p>And filtering out the junk like so:</p><pre class="brush:bash">tyler@quadjutsu:~$ locate fstab |grep etc |grep -v &quot;~&quot; |grep -v &quot;\.&quot;
/etc/fstab</pre><p>~ has a special meaning in the terminal so you must quote it to match it.  ~ refers to the user&#8217;s home directory, in this case /home/tyler.  I&#8217;ve also had to escape . because it too has special meaning, in the context of regular expressions, it will match any character&#8230; if we&#8217;re excluding any character, we&#8217;re excluding our results :).</p><h3>Quick Tricks</h3><p>Remember commands is sometimes a hard thing to do, especially when it&#8217;s a whole string of complicated commands and regexes.  Sometimes, I know I don&#8217;t have the mind to take notes but that&#8217;s okay (for a grace period) because bash keeps track for me.  The history command will list all your recently executed commands and utilizing the grep command, we can filter that list.</p><pre class="brush:bash">tyler@quadjutsu:~$ history |grep locate
  613  locate fstab
  614  locate fstab |grep etc
  624  locate fstab |grep etc |grep -v &quot;~&quot; |grep -v &quot;\.&quot;
</pre><p>A quick way to search for files in a directory would be to use locate as the path and grep the results</p><pre class="brush:bash">tyler@quadjutsu:~$ locate ~ |grep notes.txt
/home/tyler/Desktop/notes.txt
</pre><p>These are the basics of finding files and searching through them.  Linux provides many tools to reformat and replace strings.  There are a million different ways to combine commands and always a faster way to do it.  Keep playing and you&#8217;ll just get better and better.</p> ]]></content:encoded> <wfw:commentRss>http://www.doknowevil.net/2009/02/04/finding-files-and-strings-using-the-terminal-in-linux/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Object Caching 385/393 objects using disk

Served from: www.doknowevil.net @ 2012-02-07 08:27:18 -->
