
Linux Xargs 命令
如何使用xargs命令 语法: xargs [OPTIONS] [COMMAND [initial-arguments]] 举一个例子:我们用管道符传输到xargs,并为每个参数运行touch命令, -t表示在执行之前先打印,创建三个文件: [root@localhost ~]# echo "file1 file2 file3"|xargs -t touch touch file1 file2 file3 如何限制参数的数量 默认情况下,传递给命令的参数数量由系统限制决定。 -n选项指定要传递给命令的参数个数。xargs根据需要多次运行指定的命令,直到所有参数都用完为止。 下面例子指定每次传递一个参数: [root@localhost ~]# echo "file1 file2 file3"|xargs -n1 -t touch touch file1 touch file2 touch file3 如何运行多个命令 要使用xargs运行多个命令,请使用 -i或者 -I选项。在 -i或者 -I后面自定义一个传递参数符号,所有匹配的项都会替换为传递给xargs的参数。 下面例子时xargs运行两条命令,先touch创建文件,然后ls列出来: [root@localhost ~]# echo "file1 file2 file3"|xargs -t -I % sh -c 'touch %;ls -l %' sh -c touch file1 file2 file3;ls -l file1 file2 file3 -rw-r--r--. 1 root root 0 Jan 30 00:18 file1 -rw-r--r--. 1 root root 0 Jan 30 00:18 file2 -rw-r--r--. 1 root root 0 Jan 30 00:18 file3 如何指定一个分隔符 ...