<?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>Ruy Rocha &#187; Linux</title>
	<atom:link href="http://ruyrocha.com/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://ruyrocha.com</link>
	<description>As Diehard As They Come</description>
	<lastBuildDate>Tue, 27 Jul 2010 17:21:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Comando find no Linux</title>
		<link>http://ruyrocha.com/linux-comandos-find/</link>
		<comments>http://ruyrocha.com/linux-comandos-find/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 20:35:30 +0000</pubDate>
		<dc:creator>Ruy Rocha</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[comandos linux]]></category>
		<category><![CDATA[find -maxtype]]></category>
		<category><![CDATA[find -mintype]]></category>
		<category><![CDATA[man find]]></category>

		<guid isPermaLink="false">http://ruyrocha.com/?p=36</guid>
		<description><![CDATA[Inaugurando a nova seção (Comandos), com um breve post sobre como procurar arquivos no Linux. O find (man find) tem algumas coisas bem úteis. Por exemplo, você pode buscar por arquivos, por diretórios, por links simbólicos, pelo nome do arquivo sem diferenciar maiúsculas e minúsculas &#8211; lembrando que sistemas *NIX são case sensitive, etc. A [...]]]></description>
			<content:encoded><![CDATA[<p>Inaugurando a nova seção (Comandos), com um breve post sobre  como procurar arquivos no Linux.</p>
<p>O find (man find) tem algumas coisas bem úteis. Por exemplo, você pode buscar por arquivos, por diretórios, por links simbólicos, pelo nome do arquivo sem diferenciar maiúsculas e minúsculas &#8211; lembrando que sistemas *NIX são <em>case sensitive</em>, etc.<span id="more-36"></span></p>
<p>A sintaxe do find é:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> caminho expressao</pre></div></div>

<p>O caminho é onde você quer procurar, sendo relativo ou absoluto. Agora, com a expressão, podemos fazer as coisas legais. A expressão -type no diz de que tipo é o arquivos, veja:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> f
<span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> d
<span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> l</pre></div></div>

<p>Os três comandos acima buscariam, a partir do diretório em que você está, por arquivos, diretórios e links simbólicos, respectivamente.</p>
<p>Agora, para casar o tipo do arquivo com o nome, são usadas os parâmetros -name ou -iname (<em>ignore case</em>):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;nome&quot;</span> <span style="color: #660033;">-type</span> f
<span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-iname</span> <span style="color: #ff0000;">&quot;*nom?.backup&quot;</span> <span style="color: #660033;">-type</span> d</pre></div></div>

<p>E aquele asterisco? E a interrogação? É possível utilizar os metacaractéres do shell. Isto, os wildcards, os coringas. Os mesmos que você usaria num <em>ls</em> da vida.</p>
<p>Suponhamos que você tem a seguinte estrutura de diretórios:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>a<span style="color: #000000; font-weight: bold;">/</span>d1
.<span style="color: #000000; font-weight: bold;">/</span>a<span style="color: #000000; font-weight: bold;">/</span>d2
.<span style="color: #000000; font-weight: bold;">/</span>a<span style="color: #000000; font-weight: bold;">/</span>d3
.<span style="color: #000000; font-weight: bold;">/</span>b<span style="color: #000000; font-weight: bold;">/</span>d
.<span style="color: #000000; font-weight: bold;">/</span>c<span style="color: #000000; font-weight: bold;">/</span>d1
.<span style="color: #000000; font-weight: bold;">/</span>c<span style="color: #000000; font-weight: bold;">/</span>d2
.<span style="color: #000000; font-weight: bold;">/</span>c<span style="color: #000000; font-weight: bold;">/</span>d3
.<span style="color: #000000; font-weight: bold;">/</span>c<span style="color: #000000; font-weight: bold;">/</span>d4
.<span style="color: #000000; font-weight: bold;">/</span>d<span style="color: #000000; font-weight: bold;">/</span>d
.<span style="color: #000000; font-weight: bold;">/</span>e<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Os diretórios representados por letras são grupos, e dentro temos as categorias de cada grupo. O que eu preciso saber é quantas categorias (diretórios) de grupo existem, sem contar os diretórios dentro das categorias . E agora? Temos os parâmetros -mindepth e -maxdepth. Eles especificam o nível mínimo e máximo para realizar a busca. Neste caso:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-mindepth</span> <span style="color: #000000;">2</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">2</span> <span style="color: #660033;">-type</span> d</pre></div></div>

<p>Explicando melhor:</p>
<ul>
<li>o nível 0 casa com a linha de comando, ou seja, o ./</li>
<li>o nível 1 casa os nomes de grupo, como ./a e ./b</li>
<li>o nível 2 casa o que eu quero, que são as categorias, como ./a/1 e ./a/2</li>
<li>se usasse apenas o -mindepth 2, a busca continuaria dentro dos diretórios, porque eu disse apenas de onde queria começar a busca, e por isso o -maxdepth é necessário</li>
</ul>
<p>Outras iterações virão em breve. Até <img src='http://ruyrocha.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ruyrocha.com/linux-comandos-find/feed/</wfw:commentRss>
		<slash:comments>0</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)
Database Caching 1/7 queries in 0.002 seconds using disk
Object Caching 632/640 objects using apc

Served from: ruyrocha.com @ 2010-07-31 08:40:30 -->