linux 【查找替换复制】总结

  1. 内容查找并且复制
find ./ -name output.xml | grep -v plugin |xargs -i cp --parents -rf {}  /amize
{}代表cp这个命令的第一个参数


  1. 文件名或者目录查找
区别(注意find中.是内容,直接用*就行)
find ./ -type f | grep -i 123           //结果只有文件名
find ./ -type d | grep -i mysql         //查找目录
  1. 内容查找
#find
区别(注意find中.是内容,直接用*就行)
find ./ -type f | xargs grep -i 123     //结果包含文件名和内容
find ./ -type f | xargs grep -i 123 -l  //结果包含文件名和内容,只打印文件名

find ./ -type f | xargs grep '\[ERROR\]\ WSREP'  //搜索带有空格等特殊字符的grep用法
find ./ -type f | xargs grep -i dns | sed -rn '/(8.8.8.8|10.155.106.199)/p'  //结合sed 

忽略文件夹查找   
方法1
find . -path './logs' -prune -o -name ' *.pl' -print | xargs grep 123         //注意"."不是正则表达是就是正常的文本
find /usr/local/pf/ -path '/usr/local/pf/logs' -prune -o -print  -type f     
方法2
find /usr/local/pf/   -type f | xargs grep pfmon |grep -v '/usr/local/pf/logs/'


# grep
当前目录下搜索iptables 
grep iptables -rn . | grep brd_redirect


  1. 内容替换
# grep和sed替换文件中的字符串
sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ./`
将当前目录下的所有.c、.h文件中的str1字符串替换为str2字符串。

#参数解释: 
#sed:
-i 表示操作的是文件,``括起来的grep命令,表示将grep命令的的结果作为操作文件
s/"str1"/"str2"/表示查找str1并替换为str2,后面跟g表示一行中有多个str1的时候,都替换,而不是仅替换第一个
#grep:
-r表示查找当前目录以及所有子目录
-l表示仅列出符合条件的文件名,传给sed命令做替换操作
--include="*.[ch]" 表示仅查找.c、.h文件


原文地址:https://www.cnblogs.com/amize/p/14078672.html