<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[实践OK]Linux下使用find命令使用-exec 进行两次大括号传入路径进行cat重定向到对应的特定文件,清空日志目录日志。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Sat, 24 Jun 2017 05:06:30 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	[实践OK]Linux下使用find命令使用-exec 进行两次大括号传入路径进行cat重定向到对应的特定文件可结合xargs 相关命令：http://jackxiang.com/post/6520/<br/>find超级查找，路径模糊、文件名模糊、时间、大小：<br/><textarea name="code" class="php" rows="15" cols="100">
find /data/ttserver/tt_2121*/ulog -type f -name &quot;*.ulog&quot; -mtime +30&nbsp;&nbsp;-size +512M 
#Ansible: clean xiyou ttserver&#039;s 30 day ago ulogs folder
59 23 * * * find /data/ttserver/tt_2121*/ulog -type f -name &quot;*.ulog&quot; -mtime +30 -size +256M -exec rm -rf &#123;&#125; &#92;;
#Ansible: clean xiyou ttserver&#039;s 30 day ago ulogs folder
59 23 * * * find /data/logs/ttserver/ulog2121* -type f -name &quot;*.ulog&quot; -mtime +30 -size +256M -exec rm -rf &#123;&#125; &#92;;
</textarea><br/>看文件名：https://linux.cn/article-6956-1.html ,使用 -l 选项可以只显示文件名。 ll&nbsp;&nbsp;&#124;grep ^d 只显示目录。<br/>查找所有Conf文件包含了upstream的时间变量的文件列出来：<br/><textarea name="code" class="php" rows="15" cols="100">
find . -name &quot;*.conf&quot; -exec bash -c &quot;grep -l &#039;upstream_response_time&#039; &#123;&#125;&quot; &#92;;
</textarea><br/>./nginx.conf<br/><br/>#删除/home/git/gitlab下所有系统日志(每周日11点59分删除一次所有日志)<br/>59 23 * * 0 find /home/git/gitlab/log -name &quot;*.log&quot; -exec bash -c &quot;echo &#039;&#039; &gt; &#123;&#125;&quot; &#92;;<br/><textarea name="code" class="php" rows="15" cols="100">
查看：
find /data/logs -name &quot;*.log*&quot; -exec bash -c &quot;du -sh &#123;&#125;&quot; &#92;;
清空：
find /data/logs -name &quot;*.log*&quot; -exec bash -c &quot;echo &#039;&#039; &gt; &#123;&#125;&quot; &#92;; 
</textarea><br/><br/>=============================================<br/>这个解决方法我可是找了好久。。。正确写法：<br/><textarea name="code" class="php" rows="15" cols="100">
find . -name &quot;*.log&quot; -exec sh -c&nbsp;&nbsp;&#039;echo &quot;&quot; &gt; &#123;&#125;&#039; &#92; ; 
</textarea><br/><br/>find -name &quot;*&quot; -exec sh -c &#039;cat &#123;&#125; &gt; &#123;&#125;.out.iso&#039; &#92;;<br/>find -name &quot;*.php&quot; -exec bash -c &quot;mv -f &#123;&#125; &#123;&#125;.tmp&quot; &#92;; <br/>find -name &quot;*.php&quot; -exec bash -c &quot;copy &#123;&#125; &#123;&#125;.tmp&quot; &#92;; <br/>find -name &quot;*.php&quot; -exec bash -c &quot;sed -e &#039;s/aaa/bbb/&#039; &#123;&#125; &gt; &#123;&#125;.tmp; mv -f &#123;&#125;.tmp &#123;&#125;&quot; &#92;;<br/><br/>替换文件名内容：<br/><br/>find /tmp/exectest&nbsp;&nbsp;-name &quot;*.txt&quot; -exec bash -c &quot;ls &#123;&#125;&quot; &#92;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>/tmp/exectest/b.txt<br/>/tmp/exectest/aaa/ccc.txt<br/><br/>find /tmp/exectest&nbsp;&nbsp;-name &quot;*.txt&quot; -exec bash -c &quot;ls &#123;&#125;&#124;sed -e &#039;s/txt/php/&#039;&quot; &#92;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>/tmp/exectest/b.php<br/>/tmp/exectest/aaa/ddd.php<br/><br/><br/><br/><br/><br/><br/>批量给不同层次的目录包含的*.log里写入jackwriteLog或清空日志：<br/>find -name &quot;*.log&quot; -exec bash -c &quot;echo &#039;&#039; &gt; &#123;&#125;&quot; &#92;; <br/>#删除/home/git/gitlab下所有系统日志(每周日11点59分删除一次所有日志)<br/>59 23 * * 0 find /home/git/gitlab/log -name &quot;*.log&quot; -exec bash -c &quot;echo &quot;&quot; &gt; &#123;&#125;&quot; &#92;;<br/>修改为这样：<br/>#删除/home/git/gitlab下所有系统日志(每周日11点59分删除一次所有日志)<br/>find /home/git/gitlab/log -name &quot;*.log&quot; -exec bash -c &quot;echo &quot;&quot; &gt; &#123;&#125;&quot; &#92;;<br/><br/><br/><br/># find -name &quot;*.log&quot; -exec bash -c &quot;echo &#039;jackwriteLog&#039; &gt; &#123;&#125;&quot; &#92;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/># cat&nbsp;&nbsp;./k.log<br/>jackwriteLog<br/># cat&nbsp;&nbsp;./n/n.log <br/>jackwriteLog<br/># cat&nbsp;&nbsp;./n/j/a.log <br/>jackwriteLog<br/><br/><br/>find -name &quot;*.php&quot; -exec bash -c &quot;cp &#123;&#125; &#123;&#125;.tmp&quot; &#92;;&nbsp;&nbsp; <br/>a.php&nbsp;&nbsp;a.php.tmp&nbsp;&nbsp;b.php&nbsp;&nbsp;b.php.tmp<br/><br/><br/><br/>@ 2010-08-20 BS一下不看manpage的自己<br/>引用<br/>$ man sed<br/>...<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-i[SUFFIX], --in-place[=SUFFIX]<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edit files in place (makes backup if extension supplied)<br/>...<br/>也就是说，只需要用 sed -i 就可以直接替换文件中的内容<br/><br/>======以前的分割线======<br/><br/>要用sed批量修改文件的内容，但是用这个命令解决不了问题：<br/>find -name &quot;*.php&quot; -exec sed -e &#039;s/aaa/bbb/&#039; &#123;&#125; &gt; &#123;&#125; &#92;;<br/>因为bash把 &gt; 解释为find命令输出的重定向。<br/>修改一下：<br/>find -name &quot;*.php&quot; -exec sed -e &#039;s/aaa/bbb/&#039; &#123;&#125; &#92;&gt; &#123;&#125; &#92;;<br/>还是不行，因为 sed 去寻找一个名为 &gt; 的文件进行处理<br/>再修改：<br/>find -name &quot;*.php&quot; -exec &quot;sed -e &#039;s/aaa/bbb/&#039; &#123;&#125; &#92;&gt; &#123;&#125; &quot; &#92;;<br/>还是不行，因为find去找一个名为 &quot;sed -e &#039;s/aaa/bbb/&#039; &#123;&#125; &#92;&gt; &#123;&#125; &quot; 的程序来执行<br/>那是囧之又囧阿。于是去baidu，去google，<br/>但是无论baidu还是google &quot;find -exec 重定向&quot;<br/>都搜不到相应的解决方案，这样的问题居然没有人遇到过？<br/>于是用google搜了一下<br/>引用<br/>linux find using &quot;-exec&quot; sed redirect<br/><br/>搜到了这一页： http://www.loisch.de/linux.html<br/>看来还是国人太ooxx了，sigh。<br/><br/>解决方案其实很简单：绕个弯，把 -exec 的命令给shell来执行<br/>注意：下面的命令一定不要在有用的文件上直接尝试！！！！！！<br/>引用<br/>find -name &quot;*.php&quot; -exec bash -c &quot;sed -e &#039;s/aaa/bbb/&#039; &#123;&#125; &gt; &#123;&#125;&quot; &#92;;<br/><br/>哦也！没有错误提示了！<br/>然后 ls -al 一下，爽！所有文件大小都变成 0 了！<br/>为什么捏？那篇文章里面解释了：<br/>因为bash检测到需要重定向到那个文件，所以事先把那个文件清空了。<br/>那篇文章里面提到一个修改bash配置的解决方案，但是不通用，建议还是用他说的第二种方案：<br/>引用<br/>find -name &quot;*.php&quot; -exec bash -c &quot;sed -e &#039;s/aaa/bbb/&#039; &#123;&#125; &gt; &#123;&#125;.tmp; mv -f &#123;&#125;.tmp &#123;&#125;&quot; &#92;;<br/><br/>嘿，这下爽了！<br/>From:https://www.felix021.com/blog/read.php?1465<br/><br/><br/><br/><br/>像下面这些文章只是说这么用，但从不说为何，在QQ群里说只一句重定向，没一个解释更深入点的，附录：<br/>如何在-exec参数中使用重定向<br/>http://www.2cto.com/os/201306/222794.html<br/><br/>Clear log file content without impacting service that is running (in batch):<br/><br/>find . -name &quot;*.log&quot; -exec bash -c &quot;&gt;&#123;&#125;&quot; &#92;;<br/><br/>Subsitute file content in batch:<br/><br/>find -name &quot;*.xml&quot; -exec bash -c &quot;sed -e &#039;s/aaa/bbb/&#039; &#123;&#125; &gt; &#123;&#125;&quot; &#92;;<br/><br/>http://blog.csdn.net/duanlove/article/details/8261677
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]Linux下使用find命令使用-exec 进行两次大括号传入路径进行cat重定向到对应的特定文件,清空日志目录日志。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>