多个文件,按照规则分别拷贝到不同的目录
比如带ex的拷贝到ex目录,剩下的拷贝到另一个目录
find /opt/test/ -type f -name "*.txt" | xargs -i cp {} /tmp
//查询包含ex或者xsfl的拷贝到ex目录
find /root/0412 -type f -name "TOSCA_*.tar.gz" |grep -E 'EX|xsfl' | xargs -i cp {} /root/0412/ex
//查询不含ex和xsfl的拷贝到normal目录
find /root/0412 -type f -name "TOSCA_*.tar.gz" |grep -v EX|grep -v xsfl | xargs -i cp {} /root/0412/normal
grep -E '^w|^a|h$' a.txt ## 提取以w开头或者以a开头或者以h结尾的行
grep -i 查询包含的
grep -v 查询不包含的
grep -E 'a|b' 查询包括a或者b的
小写d,大写G,清空文件
匹配 YYYY-MM-DD
grep "[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}" 0131.sh
匹配ip :
grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" 0131.sh
192.168.0.1
查看/etc/passwd 匹配前5个,按:分割,取第一个1,
cat /etc/passwd|head -n 5|awk -F: '{print $1}'|grep -v root
grep -v 过滤root
cut -f 2 0132.sh 截取第2列,cut默认按照tab分割
grep "bin/bash" /etc/passwd | grep -v "root" | cut -d ":" -f 1
按照:截取第一个列。过滤root
cut -d ":" -f 1,3 /etc/passwd |grep -v root
awk '{print $1 "\t' $3}' 0132.sh
df -h | grep "/dev/vda1" |awk '{print $5}' df -h 匹配目录,获取第5列。
df -h | grep "/dev/vda1" |awk '{print $5}' |cut -d "%" -f 1 匹配目录,获取第5列,然后再%分割,取第一列
cat 0132.sh |awk '$2>29 {print $3}' |grep -v name
sed -n '2p' 0132.sh 打印第二行
sed '2,4d' 0132.sh 删除第二行到第4行
sed '2athis is james' 0132.sh 第二行后面插入
sed '2ithisi is james' 0132.sh 第二行前面插入
sed -i 's/aonier/maidi/g' 0133.sh 替换字符串
1、通配符匹配文件名
2、正则一般匹配文件内容
3、? 匹配一个字符
* 匹配任意字符
【12】 匹配1或者2
ls can?ls 匹配 cangls 或者canyls
ls can[gy]ls
4 、grep a* test.txt 匹配test.txt 所有字符
grep aa* 匹配至少一个a
grep ^a* 匹配a开头的字符
5、grep 's..d' 匹配sd之间 2个字符
6、grep 'c$' test.sh 匹配c结尾的字符
7、grep '^$' test.sh 匹配空白行
grep -n '^$' test.sh 匹配空白行并显示行号
8、grep "[0-9]" test.sh 匹配0-9数字
grep "^[0-9]" 数字开头
grep "^[0-9]$" 数字结尾
grep "[^a-z]" cangls 中括号里面表示取反,这个表示 不是纯字母,意思是包含有数字的
grep "^[^a-z]" 非小写字母开头的
grep "^[^a-zA-Z]" 表示数字开头的行
9、grep ".$' test.sh 任意字符结尾的,不含空白行
grep "\.$' test.sh 加了转义符,表示.结尾的字符
grep "a\{3\}" tet.sh 匹配至少出现3次a