<?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>TuxArena &#187; console</title>
	<atom:link href="http://www.tuxarena.com/tag/console/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tuxarena.com</link>
	<description>Ubuntu/Debian/Mint news and tutorials &#124; Linux stuff</description>
	<lastBuildDate>Sat, 11 May 2013 13:35:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Tutorial: Using the &#8216;find&#8217; Command</title>
		<link>http://www.tuxarena.com/2012/03/tutorial-using-the-find-command/</link>
		<comments>http://www.tuxarena.com/2012/03/tutorial-using-the-find-command/#comments</comments>
		<pubDate>Sat, 31 Mar 2012 16:05:14 +0000</pubDate>
		<dc:creator>Craciun Dan</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://www.tuxarena.com/?p=2040</guid>
		<description><![CDATA[GNU find is a powerful command-line utility that lets you search for files and folders in a hierarchical tree directory structure. It is the backend for all those utilities out there like the graphical searching in KDE or GNOME. However, find can be a little hard to handle at first by beginners. In this tutorial [...]]]></description>
				<content:encoded><![CDATA[<p>GNU find is a powerful command-line utility that lets you search for files and folders in a hierarchical tree directory structure. It is the backend for all those utilities out there like the graphical searching in KDE or GNOME. However, find can be a little hard to handle at first by beginners. In this tutorial I will try to explain some of the capabilities of find, show some useful one-liners and provide more explanations regarding this command.</p>
<p><span id="more-2040"></span></p>
<p>In this tutorial I will start from the basic ways of using find and head up into showing more complicated (but very useful) ways of getting the most out of it, in order to search and display exactly the results that you are looking for. The version of find that I currently have installed is 4.4.2, as it comes with Ubuntu 12.04 Precise Pangolin, and Bash 4.2.20 (older versions should work without problem too). Special thanks go to http://www.commandlinefu.com/ for some really great one-liners.</p>
<p>If you&#8217;re not familiar with the terminal, command-line or Linux in general I suggest you read my introductory tutorial here: <a href="http://www.tuxarena.com/static/intro_linux_cli.php">Introduction to Linux Command-Line for Beginners</a>.</p>
<div class="subtitle">The Basics</div>
<p>The simplest way of using find is by typying it in a terminal:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find
</textarea></pre>
<p>This will list all the files and folders (including hidden ones and their sub-files and sub-folders) in the current directory, following the whole hierarchical structure. This will usually generate a long list of files and doesn&#8217;t seem to give us much. It&#8217;s exactly the same as:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find .
</textarea></pre>
<p>Where . is the currently working directory. This will list all the files and folders in the currently working directory.</p>
<p>It&#8217;s probably best to use a new folder somewhere in the file system to see this in effect, a folder which doesn&#8217;t have many sub-folders and files.</p>
<p>Moving on, let&#8217;s search for all the files that include the name profile in their filename:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find . -name *profile*
</textarea></pre>
<p><b>*</b> is a wildcard that replaces any number of characters or no character. The above command searches in the current folder for the name <b>*profile*</b>.</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr/share -name FreeSans*
</textarea></pre>
<p>This will search inside <b>/usr/share</b> for all the files that start with FreeSans (and end in whatever characters e.g. FreeSans.ttf). I recommend using double quotes around the pattern to search for e.g. <b>find . -name &#8220;.bash*&#8221;</b>.</p>
<p>Another example:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr/share -name FreeSans* | grep Oblique
</textarea></pre>
<p>So now you know how to search for a certain filename in a specific location.</p>
<div class="subtitle">Uppercase/Lowercase</div>
<p>Sometimes you need to ignore uppercase and lowercase and just search for text by ignoring case-sensitive. We&#8217;ll to this just by replacing <b>-name</b> with <b>-iname</b>:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr/share -iname FREESANS*
</textarea></pre>
<div class="subtitle">Date</div>
<pre><textarea cols="78" rows="1" wrap="off">
find . -mtime +3 -iname *somefile*
</textarea></pre>
<p>This will search for files that were created earlier than 3 days ago.</p>
<div class="subtitle">Get only the filename instead of whole path to the file</div>
<p>find will return the whole path to the files that match the search pattern, so in order to get only the filename you can use the <b>printf</b> argument:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr/bin -name "alsa*"
</textarea></pre>
<p>To get only the filename, use:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr/bin -name "alsa*" -printf "%f\n"
</textarea></pre>
<div class="subtitle">By size</div>
<p>To search for files by size, use the <b>-size</b> argument, for example:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr -size +500k -name "*png"
</textarea></pre>
<p>This will search inside /usr for files which are equal to or larger than 500 KB and are ending in png. Another example:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr -size +1M -name "*png"
</textarea></pre>
<p>Which will search for files which are bigger than 1 MB in size. Instead of the plus sign, you could use minus in order to search for files that are smaller than a specified size:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr -size -10c -name "*png"
</textarea></pre>
<p>The -10c specifier tells find to only display files which are smaller than 10 bytes. Don&#8217;t forget the + or &#8211; preceding the desired filesize.</p>
<div class="subtitle">Automatically list details about the found files</div>
<p>You could use a pipe and the xargs command for this:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find /usr/lib -size +2M -name "*.so" | xargs ls -lh
</textarea></pre>
<p>Notice that this will list the files in the current directory if find returns no file.</p>
<div class="subtitle">Searching for files than contain specific text</div>
<p>This is probably one of the most useful ways to search for some file which name you&#8217;ve forgot but you know some of the text it contains inside.</p>
<pre><textarea cols="78" rows="1" wrap="off">
find . -name "*bash*" -exec grep -l "aliases" {} +
</textarea></pre>
<p>This will search in all the files that contain the patter bash for the word <b>aliases</b>. Those files that contain this pattern will be printed out.</p>
<div class="subtitle">Some useful one-liners</div>
<p>Find top 20 largest files:</p>
<pre><textarea cols="78" rows="1" wrap="off">
find . -type f -print0 | xargs -0 du -h | sort -hr | head -20
</textarea></pre>
<div class="subtitle">References</div>
<p>Special thanks go to <a href="http://www.commandlinefu.com/">http://www.commandlinefu.com/</a> and the following articles:</p>
<ul>
<li><a href="https://www.linux.com/learn/tutorials/316404-10-tips-for-using-gnu-find">https://www.linux.com/learn/tutorials/316404-10-tips-for-using-gnu-find</a>
<li><a href="http://www.ibm.com/developerworks/aix/library/au-unix-find.html">http://www.ibm.com/developerworks/aix/library/au-unix-find.html</a>
<li><a href="http://www.linux.ie/newusers/beginners-linux-guide/find.php">http://www.linux.ie/newusers/beginners-linux-guide/find.php</a>
<li><a href="http://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/">http://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tuxarena.com/2012/03/tutorial-using-the-find-command/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
