xargs 命令 - linux

来源:互联网 发布:浪潮gs5.0 软件下载 编辑:程序博客网 时间:2024/05/18 09:08

作者: 李云鹏(qqliyunpeng@sina.cn)
版本号: 20170124
更新时间: <2017-01-24>
原创时间: <2016-03-21>
版权: 本文采用以下协议进行授权,自由转载 - 非商用 - 非衍生 - 保持署名 | Creative Commons BY-NC-ND 3.0,转载请注明作者及出处.

一、Linux 内核的 Makefile 中有这样一句:

headerdep:    $(Q)find $(srctree)/include/ -name '*.h' | xargs --max-args 1\    $(srctree)/scripts/headerdep.pl -I$(srctree)/include

二、简单介绍:

xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具。它擅长将标准输入数据转换成命令行参数,xargs能够处理管道或者stdin并将其转换成特定命令的命令参数。xargs也可以将单行或多行文本输入转换为其他格式,例如多行变单行,单行变多行。xargs的默认命令是echo,空格是默认定界符。这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。xargs是构建单行命令的重要组件之一。

1. 选项:

如果有一个 test 文件:

a b c d e f g h i j k l m no p q r s tu v w x y z

如果

cat test | xargs

则输出单行:

a b c d e f g h i j k l m n o p q r s t u v w x y z

-n 选项,定义每行的个数:

cat test.txt | xargs -n3a b c d e f g h i j k l m n o p q r s t u v w x y z

-d 选项,自定义一个定界符:

echo "axiaoming:bxiaofeng:cxiaohei" | xargs -d:axiaoming bxiaofeng cxiaohei

–max-args 选项,定义每次传给后边命令的个数:

–max-args=max-args
-n max-args
Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the
size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
有 myecho.sh:

#!/bin/bashecho $1

在如下目录:

.
├── cpp
│ ├── add.cpp
│ ├── a.out
│ ├── class1.cpp
│ └── class.cpp
├── Kconfig
├── MAINTAINERS
├── Makefile
├── myecho.sh
├── test2.c
├── test3.c
├── test4
├── test4.c
├── test5
├── test5.c
├── test.c
└── tt.c

执行:

$find -name "*.c" | xargs --max-args 1 ./myecho.sh./test2.c./test4.c./tt.c./test3.c./test.c./test5.c

如果还不清楚,进一步的更改 myecho.sh:

#!/bin/bashecho --1--echo $1echo --2--echo $2

执行:

$ find -name "*.c" | xargs --max-args 2 ./myecho.sh--1--./test2.c--2--./test4.c--1--./tt.c--2--./test3.c--1--./test.c--2--./test5.c

参考:xargs 命令

回到索引

0 0
原创粉丝点击