<?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>Juan Benavides &#187; find</title>
	<atom:link href="http://juanbenavides.info/tag/find/feed/" rel="self" type="application/rss+xml" />
	<link>http://juanbenavides.info</link>
	<description>Anotaciones de un Geek</description>
	<lastBuildDate>Tue, 25 Oct 2011 05:08:29 +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>Buscar y reemplazar texto en muchos archivos</title>
		<link>http://juanbenavides.info/buscar-y-reemplazar-texto-en-muchos-archivos/2009/12/</link>
		<comments>http://juanbenavides.info/buscar-y-reemplazar-texto-en-muchos-archivos/2009/12/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 11:47:13 +0000</pubDate>
		<dc:creator>Juan Benavides</dc:creator>
				<category><![CDATA[Programación]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://juanbenavides.info/?p=257</guid>
		<description><![CDATA[Hay muchas formas de hacerlo y seguramente muchos IDEs ya lo hacen. Creo que Textmate, Notepad++ y Textedit son los únicos editores de código ligeros que lo hacen. Terrible que uno sea solo para OS X y los otros dos para Windows. La gente asume que todos los linuxeros usamos la consola y las expresiones [...]]]></description>
			<content:encoded><![CDATA[<p>Hay muchas formas de hacerlo y seguramente muchos IDEs ya lo hacen. Creo que Textmate, Notepad++ y Textedit son los únicos editores de código ligeros que lo hacen. Terrible que uno sea solo para OS X y los otros dos para Windows. La gente asume que todos los <em>linuxeros</em> usamos la consola y las expresiones regulares para todo.</p>
<p>Bueno, quizás lo primero si. Pero usar expresiones regulares siempre ha sido un dolor de cabeza para mi (no se como pasé Compiladores I). En busca de una solución express, tiré código a lo idiota y he aquí la idiotez llamada <strong>replace.php</strong> que me ha sacado de apuro:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'argv'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$find</span> <span style="color: #339933;">=</span>		<span style="color: #0000ff;">'&lt;td class=&quot;pie&quot;&gt;&lt;a href=&quot;#&quot;&gt;Políticas de privacidad&lt;/a&gt; &lt;a href=&quot;#&quot;&gt;Vigencia del catalogo&lt;/a&gt; &lt;a href=&quot;#&quot;&gt;Mapa del sitio&lt;/a&gt;&lt;/td&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$replace</span> <span style="color: #339933;">=</span>	<span style="color: #0000ff;">'&lt;td class=&quot;pie&quot;&gt;&lt;a href=&quot;/pagina/politicas-privacidad&quot;&gt;Políticas de privacidad&lt;/a&gt; &lt;a href=&quot;/pagina/terminos-condiciones&quot;&gt;Terminos y condiciones&lt;/a&gt;&lt;/td&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$new_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$find</span><span style="color: #339933;">,</span><span style="color: #000088;">$replace</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$new_data</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'argv'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &lt;-- Modificado<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'argv'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$new_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Se ejecuta desde consola para <strong>más practicidad</strong>, si, lo he dicho :D</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: #000000; font-weight: bold;">*</span>.ctp <span style="color: #660033;">-exec</span> php replace.php <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> \;</pre></div></div>

<p>El comando <code>find</code> buscará en el directorio <code>./</code> todos los archivos que terminen con la extensión <code>.ctp</code> y ejecutará mi script mandandole como argumento la ruta del archivo encontrado. El proceso es recursivo, por lo que si encuentra un archivo en varios subdirectorios la ejecución manualmente se traduciría en algo como <code>php replace.php ./views/pages/politicas.ctp</code></p>
<p>La ventaja de usarlo en consola es que podemos personalizar la elección de los archivos a modificar con una sencilla expresión regular, que se podría hacer más poderosa para un filtro más avanzado de archivos, pero igual yo he sido feliz concatenando la instrucción dos veces en una misma linea, así:</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: #000000; font-weight: bold;">*</span>.ctp <span style="color: #660033;">-exec</span> php replace.php <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> \; <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <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: #000000; font-weight: bold;">*</span>.html <span style="color: #660033;">-exec</span> php replace.php <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> \;
.<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>layouts<span style="color: #000000; font-weight: bold;">/</span>default.ctp <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>formulario.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>viajes.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>hacemos.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>contacto.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>static.catalogo.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>estamos.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>quienes.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>nuevos.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>beneficios.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>gracias.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>principal.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>unete.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
.<span style="color: #000000; font-weight: bold;">/</span>webroot<span style="color: #000000; font-weight: bold;">/</span>negocio.html <span style="color: #000000; font-weight: bold;">&lt;</span>-- Modificado
$</pre></div></div>

<p>Algo bruto, pero funciona incluso con mucho texto por buscar y mucho por reemplazar. La he usado tanto que decidí dedicarle este pequeño apunte, quizás a alguien más le puede servir.</p>
]]></content:encoded>
			<wfw:commentRss>http://juanbenavides.info/buscar-y-reemplazar-texto-en-muchos-archivos/2009/12/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Manipulando archivos con “find”</title>
		<link>http://juanbenavides.info/manipulando-archivos-con-find/2009/04/</link>
		<comments>http://juanbenavides.info/manipulando-archivos-con-find/2009/04/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 13:41:43 +0000</pubDate>
		<dc:creator>Juan Benavides</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[find]]></category>

		<guid isPermaLink="false">http://juanbenavides.info/?p=180</guid>
		<description><![CDATA[El comando find es una bendición para los usuarios de Unix/Linux que trabajan con muchos archivos. En mi caso particular, estar editando cientos de scripts -y toda clase de archivos- con mi editor favorito GEdit, me deja un lamentable basurero de archivos demporales. Un ejemplo práctico: -rw-r--r-- 1 linnk linnk 2.9K 2009-04-13 02:22 put_phpftp -rw-r--r-- [...]]]></description>
			<content:encoded><![CDATA[<p>El comando <code>find</code> es una bendición para los usuarios de Unix/Linux que trabajan con muchos archivos. En mi caso particular, estar editando cientos de scripts -y toda clase de archivos- con mi editor favorito <strong>GEdit</strong>, me deja un lamentable basurero de archivos demporales. Un ejemplo práctico:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #660033;">-rw-r--r--</span> <span style="color: #000000;">1</span> linnk linnk 2.9K <span style="color: #000000;">2009</span>-04-<span style="color: #000000;">13</span> 02:<span style="color: #000000;">22</span> put_phpftp
<span style="color: #660033;">-rw-r--r--</span> <span style="color: #000000;">1</span> linnk linnk 2.9K <span style="color: #000000;">2009</span>-04-<span style="color: #000000;">13</span> 02:<span style="color: #000000;">22</span> put_phpftp~
<span style="color: #660033;">-rw-r--r--</span> <span style="color: #000000;">1</span> linnk linnk 1.9K <span style="color: #000000;">2009</span>-04-<span style="color: #000000;">12</span> <span style="color: #000000;">22</span>:<span style="color: #000000;">40</span> README.txt
<span style="color: #660033;">-rwxr-xr-x</span> <span style="color: #000000;">1</span> linnk linnk 2.4K <span style="color: #000000;">2009</span>-04-<span style="color: #000000;">13</span> 03:<span style="color: #000000;">47</span> remove_phpftp
<span style="color: #660033;">-rw-r--r--</span> <span style="color: #000000;">1</span> linnk linnk 2.4K <span style="color: #000000;">2009</span>-04-<span style="color: #000000;">13</span> 03:<span style="color: #000000;">47</span> remove_phpftp~
<span style="color: #660033;">-rwxr-xr-x</span> <span style="color: #000000;">1</span> linnk linnk 7.4K <span style="color: #000000;">2009</span>-04-<span style="color: #000000;">12</span> <span style="color: #000000;">22</span>:<span style="color: #000000;">40</span> svn2web</pre></div></div>

<p>En este caso solo tengo dos archivos temporales que quedaron olvidados y que cuando quiera arrastrar este directorio a un FTP, estos archivos se subiran haciendo más basura en mi servidor.</p>
<p>La solución a esto la encontré con <code>find</code> <em>googleando</em>, pero admito que solo la repetía sistemáticamente. Suena ridículo porque ahora entiendo bien como funciona este comando y es muy sencillo de usar, además de poderoso. La solución:</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>home<span style="color: #000000; font-weight: bold;">/</span>usuario<span style="color: #000000; font-weight: bold;">/</span>proyecto<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*~&quot;</span> <span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> \;</pre></div></div>

<p>El primer parametro de <code>find</code> es el directorio donde se va a buscar (incluyendo subdirectorios), el segundo es el parametro <code>-name</code> el cual filtra por nombre según <code>"*~"</code> (cualquier nombre que termine con <code>~</code>). El siguiente parametro es <code>-exec</code> que ejectuará el comando <code>rm {}</code> donde <code>{}</code> es el archivo encontrado por <code>find</code> y termina el comando con <code>\;</code></p>
<p>Para entender bien como funciona, si quitamos la parte desde <code>-exec</code>, lo que hará listarnos únicamente los archivos temporales. De esta forma comprendemos que <code>find</code> puede ejectuar comandos por cada archivo encontrado con <code>-exec</code>, como en este otro ejemplo muy útil:</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>home<span style="color: #000000; font-weight: bold;">/</span>usuario<span style="color: #000000; font-weight: bold;">/</span>proyecto<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> f <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-l</span> <span style="color: #ff0000;">'texto_buscado'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-i</span> <span style="color: #ff0000;">''</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/texto_buscado/texto_reemplazo/g'</span></pre></div></div>

<p>En este caso buscamos un <em>texto_buscado</em> y lo reemplazamos por <em>texto_reemplazo</em> en muchos archivos dentro del directorio especificado. Finalmente, <a href="http://www.wagoneers.com/UNIX/FIND/find-usage.html">las posibilidades son infinitas</a> y no lo comprendí hasta que necesité hacer algo avanzado y me puse a jugar con el comando :P</p>
]]></content:encoded>
			<wfw:commentRss>http://juanbenavides.info/manipulando-archivos-con-find/2009/04/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

