xargs命令来自于英文词组” extended arguments“的缩写,其功能是用于给其他命令传参数的过滤器。xargs命令能够处理从标准输入或管道符输入的数据,并将其转换成命令参数,也可以将单行或多行输入的文本转换成其他格式。
xargs命令默认接收的信息中,空格是默认定界符,所以可以接收包含换行和空白的内容。
语法格式: xargs [参数]
常用参数:
| -n | 多行输出 |
| -d | 自定义一个定界符 |
| -I | 指定一个替换字符串{} |
| -t | 打印出 xargs 执行的命令 |
| -p | 执行每一个命令时弹出确认 |
参考实例
默认以空格为定界符,以多行形式输出文件内容,每行显示三段内容值:
[root@linuxcool ~]# cat anaconda-ks.cfg | xargs -n 3 #version=RHEL8 ignoredisk --only-use=sda autopart --type=lvm # Partition clearing information clearpart --all --initlabel --drives=sda # Use graphical install graphical ………………省略部分输出信息………………
指定字符X为定界符,默认以单行的形式输出字符串内容:
[root@linuxcool ~]# echo "FirstXSecondXThirdXFourthXFifth" | xargs -dX First Second Third Fourth Fifth
指定字符X为定界符,以多行形式输出文本内容,每行显示两段内容值:
[root@linuxcool ~]# echo "FirstXSecondXThirdXFourthXFifth" | xargs -dX -n 2 First Second Third Fourth Fifth
设定每一次输出信息时,都需要用户手动确认后再显示到终端界面:
[root@linuxprobe ~]# echo "FirstXSecondXThirdXFourthXFifth" | xargs -dX -n 2 -p echo First Second ?...y First Second echo Third Fourth ?...y Third Fourth echo Fifth ?...y Fifth
由xargs调用执行执行的命令,并将结果输出到终端界面:
[root@linuxcool ~]# ls | xargs -t -I{} echo {}
echo anaconda-ks.cfg
anaconda-ks.cfg
echo Desktop
Desktop
echo Documents
Documents
echo Downloads
Downloads
echo initial-setup-ks.cfg
initial-setup-ks.cfg
echo Music
Music
echo Pictures
Pictures
echo Public
Public
echo Templates
Templates
echo Videos
Videos
