您的位置:首页 > 运维架构 > Shell

find,exec及xargs命令

2018-03-15 16:08 260 查看
先看几个例子

[root@localhost exec]# touch {1..5}.txt
[root@localhost exec]# touch {1..5}.sh
[root@localhost exec]# ls
1.sh  1.txt  2.sh  2.txt  3.sh  3.txt  4.sh  4.txt  5.sh  5.txt
[root@localhost exec]# find . -name '*.txt' |xargs -t -i{} rm -f {}
--------------------------------
rm -f ./1.txt
rm -f ./2.txt
rm -f ./3.txt
rm -f ./4.txt
rm -f ./5.txt
-------------------------------

[root@localhost exec]# ls
1.sh  2.sh  3.sh  4.sh  5.sh

[root@localhost exec]# find . -name '*.sh' -exec mv {} {}_bak \;
[root@localhost exec]# ls
1.sh_bak  2.sh_bak  3.sh_bak  4.sh_bak  5.sh_bak


-exec的用法较少,主要搭配find使用。

find . -name '*.sh' -exec mv {} {}_bak \;
批量更改文件名 -----------------------------------------------------------
-exec之后可以接任何命令,{}表示一个匹配。对于匹配到的文件名,{}均会被该文件名所替换。
最后的\作为;的转义,否则shell会把;当做命令的结尾。
-exec结合多个命令:
-exec只能接受单个命令,不过可以耍一个小花招。把多个命令写在一个脚本文件中,然后再-exec中使用这个脚本:
-exec ./cmds.sh {} \;
-----------------------------------------------------------


xargs的用法就更多了

有些命令只能以命令行参数的形式接受数据,而无法通过stdin(标准输入)接收数据流,在这种情况下,无法通过管道符提供给那些只有通过命令行参数接受数据的命令。
xargs擅长将标准输入转换为命令行参数。

xargs应紧跟在管道操作符之后。
--------------------------------------------------

参数选项:
-t  在执行命令时,先将命令打印出来

-n  一次处理的参数个数,不加-n就表示一次将参数传递给后面的命令

-d  为输入指定一个定制的定界符

-I/i    (如-i{} -I{} -I {} 不能写为-i {})指定替换字符串,这个替换字符串在xargs扩展时会被替换掉。使用-I/i的时候命令以循环的方式执行。如果有三个参数,那么命令就会连同{}一起被执行三次。
------------------------------------------------------
[root@localhost exec]# echo 'splitXsplitXsplitXsplit' |xargs -d X -n 2
split split
split split

[root@localhost exec]# ls
1.sh_bak  2.sh_bak  3.sh_bak  4.sh_bak  5.sh_bak

批量更改文件名
=====================================================
/tmp/1.sh
#!/bin/bash
dir=/root/exec
i=1
ls $dir |
while read line;do
mv $line ${i}.sh
let i++
done
=====================================================
[root@localhost exec]# sh /tmp/1.sh
[root@localhost exec]# ls
1.sh  2.sh  3.sh  4.sh  5.sh

[root@localhost exec]# ls *.sh | xargs ls -al
-rw-r--r-- 1 root root 0 3月  15 22:39 1.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 2.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 3.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 4.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 5.sh

解释一下上面的命令
1、首先 ls *.sh的输出为1.sh 2.sh 3.sh 4.sh 5.sh
2、通过管道的作用将1.sh 2.sh 3.sh 4.sh 5.sh 作为xargs的输入参数。
3、xargs命令接受到参数后,将其转换为一行参数 1.sh 2.sh 3.sh 4.sh 5.sh 传递给后续的命令,构成这样的命令 ls -al 1.sh 2.sh 3.sh 4.sh 5.sh

可以加上-t参数,在执行命令时,先将命令打印出来
[root@localhost exec]# ls *.sh | xargs -t ls -al
ls -al 1.sh 2.sh 3.sh 4.sh 5.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 1.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 2.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 3.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 4.sh
-rw-r--r-- 1 root root 0 3月  15 22:39 5.sh

[root@localhost exec]# ls *.sh | xargs -t  -i{}  ls {}  -al
ls 1.sh -al
-rw-r--r-- 1 root root 0 3月  15 22:39 1.sh
ls 2.sh -al
-rw-r--r-- 1 root root 0 3月  15 22:39 2.sh
ls 3.sh -al
-rw-r--r-- 1 root root 0 3月  15 22:39 3.sh
ls 4.sh -al
-rw-r--r-- 1 root root 0 3月  15 22:39 4.sh
ls 5.sh -al
-rw-r--r-- 1 root root 0 3月  15 22:39 5.sh

[root@localhost exec]# find . -name '*.sh' | xargs -t -I{} mv {} {}.bak
mv ./1.sh ./1.sh.bak
mv ./2.sh ./2.sh.bak
mv ./3.sh ./3.sh.bak
mv ./4.sh ./4.sh.bak
mv ./5.sh ./5.sh.bak
[root@localhost exec]# ls
1.sh.bak  2.sh.bak  3.sh.bak  4.sh.bak  5.sh.bak
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell