﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-信心比金钱更重要！-随笔分类-Linux</title><link>http://www.cppblog.com/luofeng225/category/9986.html</link><description>目标明确==&gt;&gt;&gt;计划跟踪==&gt;&gt;&gt;行动执行！</description><language>zh-cn</language><lastBuildDate>Sun, 29 Apr 2012 18:51:11 GMT</lastBuildDate><pubDate>Sun, 29 Apr 2012 18:51:11 GMT</pubDate><ttl>60</ttl><item><title>硬链接和符号链接(软链接) 彻底理解</title><link>http://www.cppblog.com/luofeng225/archive/2012/04/27/172888.html</link><dc:creator>luofeng</dc:creator><author>luofeng</author><pubDate>Fri, 27 Apr 2012 01:02:00 GMT</pubDate><guid>http://www.cppblog.com/luofeng225/archive/2012/04/27/172888.html</guid><wfw:comment>http://www.cppblog.com/luofeng225/comments/172888.html</wfw:comment><comments>http://www.cppblog.com/luofeng225/archive/2012/04/27/172888.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luofeng225/comments/commentRss/172888.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luofeng225/services/trackbacks/172888.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); text-indent: 28px; line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); text-indent: 28px; line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">&nbsp;<br />&nbsp;&nbsp; Linux 文件系统最重要的特点之一是它的文件链接。链接是对文件的引用，这样您可以让文件在文件系统中多处被看到。保存在磁盘分区中的文件不管是什么类型都给它分配一个编号，称为索引节点号inode 。在 Linux 中，链接可以如同原始文件一样来对。链接可以与普通的文件一样被执行、编辑和访问。对系统中的其他应用程序而言，链接就是它所对应的原始文件。当您通过链接对文件进行编辑时，您编辑的实际上是原始文件。<br />链接不是副本。有两种类型的链接：硬链接和符号链接(软链接)。&nbsp;</span></span><br />&nbsp;<br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); text-indent: 28px; line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif"><strong>硬链接只</strong>能引用同一文件系统中的文件。它引用的是文件在文件系统中的物理索引(也称为 inode)。当您移动或删除原始文件时，硬链接不会被破坏，因为<strong>它所引用的是文件的物理数据而不是文件在文件结构中的位置</strong>。硬链接的文件不需要用户有访问原始文件的权限，也不会显示原始文件的位置，这样有助于文件的安全。如果您删除的文件有相应的硬链接，那么这个文件依然会保留，直到所有对它的引用都被删除。<strong><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(65,73,71); text-indent: 28px; font-family: Arial; webkit-border-horizontal-spacing: 2px; webkit-border-vertical-spacing: 2px">一个文件除非链接数为0，否则不会物理地从文件系统中被删除。</span></span><br /></strong><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); text-indent: 28px; line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">&nbsp;&nbsp; <br /><strong>软连接</strong>，其实就是新建立一个文件，这个文件就是专门用来指向别的文件的（那就和windows 下的快捷方式的那个文件有很接近的意味）。软连接产生的是一个新的文件，但这个文件的作用就是专门指向某个文件的，删了这个软连接文件，那就等于不需要这个连接，和原来的存在的实体原文件没有任何关系，但删除原来的文件，则相应的软连接不可用（cat那个软链接文件，则提示&#8220;没有该文件或目录&#8220;）。</span></span></span></span></span></span> <br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 6px; word-break: break-all; text-indent: 2em; padding-top: 0px"><br /><strong>硬链接与软链接的区别；<br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-break: break-all; padding-top: 0px" /></strong><u>&nbsp;&nbsp;&nbsp; 硬连接是不会建立inode的，他只是在文件原来的inode link count域再增加1而已，</u>也因此硬链接是不可以跨越文件系统的。相反都是软连接会重新建立一个inode，当然inode的结构跟其他的不一样，他只是一个指明源文件的字符串信息。一旦删除源文件，那么软连接将变得毫无意义。而硬链接删除的时候，系统调用会检查inode link count的数值，如果他大于等于1，那么inode不会被回收。因此文件的内容不会被删除。<br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">&nbsp; <br />　硬链接实际上是为文件建一个别名，链接文件和原文件实际上是同一个文件。可以通过ls -i来查看一下，这两个文件的inode号是同一个，说明它们是同一个文件；而软链接建立的是一个指向，即链接文件内的内容是指向原文件的指针，它们是两个文件。&nbsp;<span class="Apple-converted-space">&nbsp;</span><br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-break: break-all; padding-top: 0px" />&nbsp;　　软链接可以跨文件系统，硬链接不可以；软链接可以对一个不存在的文件名(filename)进行链接（当然此时如果你vi这个软链接文件，linux会自动新建一个文件名为filename的文件）,硬链接不可以（其文件必须存在，inode必须存在）；软链接可以对目录进行连接，硬链接不可以。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 6px; word-break: break-all; text-indent: 2em; padding-top: 0px"></span></span><br /><br />创建<br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); text-indent: 28px; line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">两种链接都可以通过命令 ln 来创建。ln 默认创建的是硬链接。使用 -s 开关可以创建软链接。</span></span><br /><br />格式：ln 文件&nbsp; 链接文件<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">&nbsp;创建的硬链接文件与原文件的文件大小相同，node值相同。文件属性为普通文件。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 6px; word-break: break-all; text-indent: 2em; padding-top: 0px">当修改任意个文件内容均可是三个文件内容同时改变。<br /></span></span><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); text-indent: 28px; line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">当删除原文件的时候硬链接文件仍然存在，且内容不变。<br /></span></span><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(65,73,71); text-indent: 28px; font-family: Arial; webkit-border-horizontal-spacing: 2px; webkit-border-vertical-spacing: 2px">&nbsp;对硬链接有如下限制：<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp; &#9670;不能对目录文件做硬链接。<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp; &#9670;不能在不同的文件系统之间做硬链接。也就是说,链接文件和被链接文件必须位于同一个文件系统中。</span></span><br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); text-indent: 28px; line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">[guo@guo ~]$ ln -s link link.back1&nbsp;<span class="Apple-converted-space">&nbsp;</span><br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-break: break-all; padding-top: 0px" />[guo@guo ~]$ ls -al |grep 'link*'&nbsp;<span class="Apple-converted-space">&nbsp;<br /><br /></span></span></span><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); text-indent: 28px; line-height: 25px; font-family: Verdana, Helvetica, Arial, sans-serif">注意此时链接的文件属性为l，软文件大小与原文件大小也不一样。node值不一样！<br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-break: break-all; padding-top: 0px" />&nbsp;<br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-break: break-all; padding-top: 0px" />与硬链接一样，当修改任意个文件内容均可是三个文件内容同时改变，此处不在举例。&nbsp;<br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-break: break-all; padding-top: 0px" />对于软链接当删除原文件后链接文件不能再打开。</span></span><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(65,73,71); line-height: 27px; font-family: Arial; webkit-border-horizontal-spacing: 2px; webkit-border-vertical-spacing: 2px">符号链接没有硬链接的限制，可以对目录文件做符号链接，也可以在不同文件系统之间做符号链接。</p>
<p style="text-indent: 2em; line-height: normal">&nbsp;<strong>&nbsp;用&#8220;ln -s&#8221;命令建立符号链接时，最好源文件用绝对路径名</strong>，这样可以在任何工作目录下进行符号链接。当源文件用相对路径时，如果当前的工作路径和你要创建的符号链接文件所在路径不同时，就不能进行链接</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 6px; word-break: break-all; text-indent: 2em; padding-top: 0px"></span></span><br /><br /><a href="http://www.hx95.com/Article/Tech/201204/54204.html">http://www.hx95.com/Article/Tech/201204/54204.html</a><br /></p></span></span><img src ="http://www.cppblog.com/luofeng225/aggbug/172888.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luofeng225/" target="_blank">luofeng</a> 2012-04-27 09:02 <a href="http://www.cppblog.com/luofeng225/archive/2012/04/27/172888.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell 笔记 tod</title><link>http://www.cppblog.com/luofeng225/archive/2012/04/26/172811.html</link><dc:creator>luofeng</dc:creator><author>luofeng</author><pubDate>Thu, 26 Apr 2012 05:56:00 GMT</pubDate><guid>http://www.cppblog.com/luofeng225/archive/2012/04/26/172811.html</guid><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(51,51,51); line-height: 25px; font-family: Arial, Tahoma, Verdana, sans-serif; text-align: left"> 
<p style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 5px; margin: 0px; line-height: 25px; padding-top: 5px"><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px"></span>&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 5px; margin: 0px; line-height: 25px; padding-top: 5px"><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px"><br /><strong>shell脚本的组成元素</strong></span><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px"><br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px" /></span></p>
<ol style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"><li style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none">
<div style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; line-height: 25px; padding-top: 0px; text-align: justify"><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px">系统命令</span><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px"><br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px" /></span><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px">文本处理工具（sort、grep、sed、awk&#8230;）<br style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px" /></span><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px">变量<br /></span><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px">条件判断</span></div></li><li style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none">
<div style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; line-height: 25px; padding-top: 0px; text-align: justify"><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px">环循结构<br /></span><span style="padding-right: 0px; padding-left: 0px; font-size: 14px; padding-bottom: 0px; margin: 0px; color: black; line-height: 25px; padding-top: 0px">函数</span></div></li></ol></span></span><img src ="http://www.cppblog.com/luofeng225/aggbug/172811.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luofeng225/" target="_blank">luofeng</a> 2012-04-26 13:56 <a href="http://www.cppblog.com/luofeng225/archive/2012/04/26/172811.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>xargs</title><link>http://www.cppblog.com/luofeng225/archive/2011/11/14/160094.html</link><dc:creator>luofeng</dc:creator><author>luofeng</author><pubDate>Mon, 14 Nov 2011 09:07:00 GMT</pubDate><guid>http://www.cppblog.com/luofeng225/archive/2011/11/14/160094.html</guid><wfw:comment>http://www.cppblog.com/luofeng225/comments/160094.html</wfw:comment><comments>http://www.cppblog.com/luofeng225/archive/2011/11/14/160094.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luofeng225/comments/commentRss/160094.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luofeng225/services/trackbacks/160094.html</trackback:ping><description><![CDATA[<div><span style="color: #696969; font-family: Arial; font-size: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; ">xargs<br style="line-height: normal; " />大多数 Linux 命令都会产生输出：文件列表、字符串列表等。但如果要使用其他某个命令并将前一个命令的输出作为参数该怎么办？例如，file 命令显示文件类型（可执行文件、ascii 文本等）；你能处理输出，使其仅显示文件名，目前你希望将这些名称传递给 ls -l 命令以查看时间戳记。xargs 命令就是用来完成此项工作的。他允许你对输出执行其他某些命令。记住下面这个来自于第 1 部分中的语法：&nbsp;<br style="line-height: normal; " /><span style="line-height: normal; color: #ff0000; ">例1：</span></span></p><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; "><span style="line-height: normal; color: #1f4799; ">file -Lz * | grep ASCII | cut -d":" -f1 | xargs ls -ltr</span><br style="line-height: normal; " />让我们来剖析这个命令字符串。第一个，file -Lz *，用于查找是符号链接或经过压缩的文件。他将输出传递给下一个命令 grep ASCII，该命令在其中搜索 "ASCII" 字符串并产生如下所示的输出：</span></p><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; "><span style="line-height: normal; color: #1f4799; ">alert_DBA102.log:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ASCII English text</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">alert_DBA102.log.Z:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ASCII text (compress&#8217;d data 16 bits)</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">dba102_asmb_12307.trc.Z: ASCII English text (compress&#8217;d data 16 bits)</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">dba102_asmb_20653.trc.Z: ASCII English text (compress&#8217;d data 16 bits)</span><br style="line-height: normal; " />由于我们只对文件名感兴趣，因此我们应用下一个命令&nbsp;<span style="line-height: normal; color: #1f4799; ">cut -d":" -f1</span>，仅显示第一个字段：</span></p><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; "><span style="line-height: normal; color: #1f4799; ">alert_DBA102.log</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">alert_DBA102.log.Z</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">dba102_asmb_12307.trc.Z</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">dba102_asmb_20653.trc.Z</span><br style="line-height: normal; " />目前，我们希望使用 ls -l 命令，将上述列表作为参数进行传递，一次传递一个。xargs 命令允许你这样做。最后一部分，<span style="line-height: normal; color: #1f4799; ">xargs ls -ltr</span>，用于接收输出并对其执行 ls -ltr 命令，如下所示：<br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">ls -ltr alert_DBA102.log</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">ls -ltr alert_DBA102.log.Z</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">ls -ltr dba102_asmb_12307.trc.Z</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">ls -ltr dba102_asmb_20653.trc.Z</span><br style="line-height: normal; " />因此，xargs 本身虽然没有多大用处，但在和其他命令相结合时，他的功能非常强大。<br style="line-height: normal; " />下面是另一个示例，我们希望计算这些文件中的行数：<br style="line-height: normal; " /><span style="line-height: normal; color: #ff0000; ">例 2：</span></span></p><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; "><span style="line-height: normal; color: #1f4799; ">$ file * | grep ASCII | cut -d":" -f1 | xargs wc -l</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">47853 alert_DBA102.log</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">&nbsp;&nbsp;&nbsp;&nbsp; 19 dba102_cjq0_14493.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">29053 dba102_mmnl_14497.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">&nbsp;&nbsp;&nbsp; 154 dba102_reco_14491.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">&nbsp;&nbsp;&nbsp;&nbsp; 43 dba102_rvwr_14518.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">77122 total</span><br style="line-height: normal; " />（注：上述任务还可用以下命令完成：）<br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">$ wc -l &#8216;file * | grep ASCII | cut -d":" -f1 | grep ASCII | cut -d":" -f1&#8216;</span><br style="line-height: normal; " />该 xargs 版本用于阐释概念。Linux 能用几种方法来完成同一个任务；请使用最适合你的情况的方法。<br style="line-height: normal; " />使用该方法，你能快速重命名目录中的文件。</span></p><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; color: #ff0000; ">例3：</span><span style="line-height: normal; font-size: 18px; "><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">$ ls | xargs -t -i mv {} {}.bak</span><br style="line-height: normal; " />-i 选项告诉 xargs 用每项的名称替换 {}。-t 选项指示 xargs 先打印命令，然后再执行。<br style="line-height: normal; " />另一个非常有用的操作是当你使用 vi 打开要编辑的文件时：<br style="line-height: normal; " /></span><span style="line-height: normal; font-size: 18px; "><span style="line-height: normal; font-size: 18px; color: #ff0000; ">例4：</span><span style="line-height: normal; font-size: 18px; "><br style="line-height: normal; " /></span><span style="line-height: normal; color: #1f4799; ">$ file * | grep ASCII | cut -d":" -f1 | xargs vi</span><br style="line-height: normal; " />该命令使用 vi 逐个打开文件。当你希望搜索多个文件并打开他们进行编辑时，使用该命令非常方便。&nbsp;<br style="line-height: normal; " />他更有几个选项。最有用的可能是 -p 选项，他使操作具有可交互性：</span></p><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; "><span style="line-height: normal; font-size: 18px; color: #ff0000; ">例5：</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">$ file * | grep ASCII | cut -d":" -f1 | xargs -p vi</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">vi alert_DBA102.log dba102_cjq0_14493.trc dba102_mmnl_14497.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">dba102_reco_14491.trc dba102_rvwr_14518.trc ?...</span><br style="line-height: normal; " />此处的 xarg 需求你在运行每个命令之前进行确认。如果你按下 "y"，则执行命令。当你对文件进行某些可能有破坏且不可恢复的操作（如删除或覆盖）时，你会发现该选项非常有用。<br style="line-height: normal; " />-t 选项使用一个周详模式；他显示要运行的命令，是调试过程中一个非常有帮助的选项。<br style="line-height: normal; " />如果传递给 xargs 的输出为空怎么办？考虑以下命令：<br style="line-height: normal; " /><span style="line-height: normal; font-size: 18px; color: #ff0000; ">例6：</span><span style="line-height: normal; font-size: 18px; "><br style="line-height: normal; " /></span><span style="line-height: normal; color: #1f4799; ">$ file * | grep SSSSSS | cut -d":" -f1 | xargs -t wc -l</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">wc -l</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">$</span><br style="line-height: normal; " />在此处，搜索 "SSSSSS" 后没有匹配的内容；因此 xargs 的输入均为空，如第二行所示（由于我们使用 -t 这个周详选项而产生的结果）。虽然这可能会有所帮助，但在某些情况下，如果没有要处理的内容，你可能希望停止 xargs；如果是这样，能使用 -r 选项：</span></p><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; "><span style="line-height: normal; font-size: 18px; color: #ff0000; ">例7：</span><span style="line-height: normal; font-size: 18px; "><br style="line-height: normal; " /></span><span style="line-height: normal; color: #1f4799; ">$ file * | grep SSSSSS | cut -d":" -f1 | xargs -t -r wc -l</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">$</span><br style="line-height: normal; " />如果没有要运行的内容，该命令退出。<br style="line-height: normal; " />假设你希望使用 rm 命令（该命令将作为 xargs 命令的参数）删除文件。然而，rm 只能接受有限数量的参数。如果你的参数列表超出该限制怎么办？xargs 的 -n 选项限制单个命令行的参数个数。&nbsp;<br style="line-height: normal; " />下面显示了怎么限制每个命令行仅使用两个参数：即使向 xargs ls -ltr 传递五个文件，但每次向 ls -ltr 仅传递两个文件。<br style="line-height: normal; " /><span style="line-height: normal; font-size: 18px; color: #ff0000; ">例8：</span><span style="line-height: normal; font-size: 18px; "><br style="line-height: normal; " /></span><span style="line-height: normal; color: #1f4799; ">$ file * | grep ASCII | cut -d":" -f1 | xargs -t -n2 ls -ltr</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">ls -ltr alert_DBA102.log dba102_cjq0_14493.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">-rw-r-----&nbsp;&nbsp;&nbsp; 1 oracle&nbsp;&nbsp; dba&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 738 Aug 10 19:18 dba102_cjq0_14493.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">-rw-r--r--&nbsp;&nbsp;&nbsp; 1 oracle&nbsp;&nbsp; dba&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2410225 Aug 13 05:31 alert_DBA102.log</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">ls -ltr dba102_mmnl_14497.trc dba102_reco_14491.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">-rw-r-----&nbsp;&nbsp;&nbsp; 1 oracle&nbsp;&nbsp; dba&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5386163 Aug 10 17:55 dba102_mmnl_14497.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">-rw-r-----&nbsp;&nbsp;&nbsp; 1 oracle&nbsp;&nbsp; dba&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6808 Aug 13 05:21 dba102_reco_14491.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">ls -ltr dba102_rvwr_14518.trc</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">-rw-r-----&nbsp;&nbsp;&nbsp; 1 oracle&nbsp;&nbsp; dba&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2087 Aug 10 04:30 dba102_rvwr_14518.trc</span><br style="line-height: normal; " />使用该方法，你能快速重命名目录中的文件。</span></p><p style="line-height: normal; "><span style="line-height: normal; font-size: 18px; "><span style="line-height: normal; color: #ff0000; ">比较实用的应用</span><br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">$ ls | xargs -t -i mv {} {}.bak</span><br style="line-height: normal; " />-i 选项告诉 xargs 用每项的名称替换 {}。</span></p><p style="line-height: normal; "></p><p style="line-height: normal; "><br style="line-height: normal; " /><span style="line-height: normal; font-size: 18px; ">删除数量比较多的文件<br style="line-height: normal; " /><span style="line-height: normal; color: #1f4799; ">ls | xargs -n 20 rm -fr</span><br style="line-height: normal; " />ls当然是输出所有的文件名(用空格分割)<br style="line-height: normal; " />xargs就是将ls的输出，每20个为一组(以空格为分隔符)，作为rm -rf的参数<br style="line-height: normal; " />也就是说将所有文件名20个为一组，由rm -rf删除，这样就不会超过命令行的长度了<br /><br /><br /><div><a href="http://hi.baidu.com/vivachn/blog/item/9cf703536fe61a08377abe9b.html">http://hi.baidu.com/vivachn/blog/item/9cf703536fe61a08377abe9b.html</a></div></span></p></span></div><img src ="http://www.cppblog.com/luofeng225/aggbug/160094.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luofeng225/" target="_blank">luofeng</a> 2011-11-14 17:07 <a href="http://www.cppblog.com/luofeng225/archive/2011/11/14/160094.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>