<?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>GlobStudio &#187; find</title>
	<atom:link href="http://blog.globstudio.com/tag/find/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.globstudio.com</link>
	<description>记录成长历程，专注软件开发</description>
	<lastBuildDate>Wed, 14 Dec 2011 07:53:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>linux下查找并删除</title>
		<link>http://blog.globstudio.com/371.html</link>
		<comments>http://blog.globstudio.com/371.html#comments</comments>
		<pubDate>Mon, 08 Feb 2010 05:24:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[开发技术]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[xargs]]></category>

		<guid isPermaLink="false">http://blog.0goshopping.com/371.html</guid>
		<description><![CDATA[find ./ -name *~ &#124; xargs rm -f 删除当前目录及子目录下的所有文件名以~结尾的文件 xargs &#8211; build and execute command lines from standard input 在使用find命令的-exec选项处理匹配到的文件时， find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制，这样在find命令运行几分钟之后，就会出现溢出错误。错误信息通常是”参数列太长”或”参数列溢出”。这就是xargs命令的用处所在，特别是与find命令一起使用。 find命令把匹配到的文件传递给xargs命令，而xargs命令每次只获取一部分文件而不是全部，不像-exec选项那样。这样它可以先处理最先获取的一部分文件，然后是下一批，并如此继续下去。 在有些系统中，使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程，并非将匹配到的文件全部作为参数一次执行；这样在有些情况下就会出现进程过多，系统性能下降的问题，因而效率不高； 而使用xargs命令则只有一个进程。另外，在使用xargs命令时，究竟是一次获取所有的参数，还是分批取得参数，以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。 来看看xargs命令是如何同find命令一起使用的，并给出一些例子。 下面的例子查找系统中的每一个普通文件，然后使用xargs命令来测试它们分别属于哪类文件 #find . -type f -print &#124; xargs file ./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text ./.kde/Autostart/.directory: ISO-8859 text\ &#8230;&#8230; 在整个系统中查找内存信息转储文件(core dump) ，然后把结果保存到/tmp/core.log 文件中： $ find / -name “core” -print &#124; xargs [...]]]></description>
			<content:encoded><![CDATA[<p>find ./ -name *~ | xargs rm -f</p>
<p>删除当前目录及子目录下的所有文件名以~结尾的文件</p>
<p>xargs &#8211; build and execute command lines from standard input</p>
<p>在使用find命令的-exec选项处理匹配到的文件时， find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制，这样在find命令运行几分钟之后，就会出现溢出错误。错误信息通常是”参数列太长”或”参数列溢出”。这就是xargs命令的用处所在，特别是与find命令一起使用。</p>
<p>find命令把匹配到的文件传递给xargs命令，而xargs命令每次只获取一部分文件而不是全部，不像-exec选项那样。这样它可以先处理最先获取的一部分文件，然后是下一批，并如此继续下去。</p>
<p>在有些系统中，使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程，并非将匹配到的文件全部作为参数一次执行；这样在有些情况下就会出现进程过多，系统性能下降的问题，因而效率不高；</p>
<p>而使用xargs命令则只有一个进程。另外，在使用xargs命令时，究竟是一次获取所有的参数，还是分批取得参数，以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。</p>
<p> <span id="more-371"></span>
<p>来看看xargs命令是如何同find命令一起使用的，并给出一些例子。</p>
<p>下面的例子查找系统中的每一个普通文件，然后使用xargs命令来测试它们分别属于哪类文件</p>
<p>#find . -type f -print | xargs file <br/>./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text <br/>./.kde/Autostart/.directory: ISO-8859 text\ <br/>&#8230;&#8230;</p>
<p>在整个系统中查找内存信息转储文件(core dump) ，然后把结果保存到/tmp/core.log 文件中：</p>
<p>$ find / -name “core” -print | xargs echo “” &gt;/tmp/core.log</p>
<p>上面这个执行太慢，我改成在当前目录下查找</p>
<p>#find . -name “file*” -print | xargs echo “” &gt; /temp/core.log <br/># cat /temp/core.log <br/>./file6</p>
<p>在当前目录下查找所有用户具有读、写和执行权限的文件，并收回相应的写权限：</p>
<p># ls -l <br/>drwxrwxrwx 2 sam adm 4096 10月 30 20:14 file6 <br/>-rwxrwxrwx 2 sam adm 0 10月 31 01:01 http3.conf <br/>-rwxrwxrwx 2 sam adm 0 10月 31 01:01 httpd.conf <br/># find . -perm -7 -print | xargs chmod o-w <br/># ls -l <br/>drwxrwxr-x 2 sam adm 4096 10月 30 20:14 file6 <br/>-rwxrwxr-x 2 sam adm 0 10月 31 01:01 http3.conf <br/>-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf</p>
<p>用grep命令在所有的普通文件中搜索hostname这个词：</p>
<p># find . -type f -print | xargs grep “hostname” <br/>./httpd1.conf:# different IP addresses or hostnames and have them handled by the <br/>./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames <br/>on your</p>
<p>用grep命令在当前目录下的所有普通文件中搜索hostnames这个词：</p>
<p># find . -name \* -type f -print | xargs grep “hostnames” <br/>./httpd1.conf:# different IP addresses or hostnames and have them handled by the <br/>./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames <br/>on your</p>
<p>注意，在上面的例子中， \用来取消find命令中的*在shell中的特殊含义。</p>
<p>find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.globstudio.com/371.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

