xargs命令使用技巧

背景

管道符 | + xargs, 用于把上游输出转换为下游参数输入。

例如 ls *.bak | xargs rm -f

问题:
xargs默认把输入作为参数放到命令的最后,但是很多命令需要自己定位参数的位置,比如拷贝命令cp {上游结果} destFolder
解决方法:
xargs -I <参数指示符>, 然后用这个参数指示符定位参数插入的位置,

ls *.bak | xargs -I % cp % /tmp/test

注释:这里使用%作为指示符,第一个%可以理解为声明,第二个%可以理解为调用。你也可以用其他字符,比如 ls *.bak | xargs -I {} cp {} /tmp/test

简介

之所以要用到xargs这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中又有这个需要,所以就有了xargs命令,例如:

1
2
find /sbin -perm +700 | ls -l       这个命令是错误的
find /sbin -perm +700 | xargs ls -l 这样才是正确的

xargs 可以读入 stdin 的资料,并且以空白字符或断行字符作为分隔,将 stdin 的资料分隔成为 arguments 。 因为是以空白字符作为分隔,所以,如果有一些文件名或者是其他意义的名词内含有空白字符的时候, xargs 可能就会误判了~

选项解释

  • -0 当sdtin含有特殊字符时候,将其当成一般字符,像/ ‘ 空格等

    1
    2
    root@localhost:~/test#echo "//"|xargs  echo
    root@localhost:~/test#echo "//"|xargs -0 echo
  • -a file 从文件中读入作为sdtin

    1
    2
    3
    4
    5
    6
    root@localhost:~/test#cat test
    #!/bin/sh
    echo "hello world/n"
    root@localhost:~/test#xargs -a test echo
    #!/bin/sh echo hello world/n
    root@localhost:~/test#
  • -e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。

    1
    2
    3
    4
    root@localhost:~/test#cat txt
    /bin tao shou kun
    root@localhost:~/test#cat txt|xargs -E 'shou' echo
    /bin tao
  • -p 当每次执行一个argument的时候询问一次用户。

    1
    2
    3
    root@localhost:~/test#cat txt|xargs -p echo
    echo /bin tao shou kun ff ?...y
    /bin tao shou kun ff
  • -n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的

    1
    2
    3
    4
    5
    6
    7
    root@localhost:~/test#cat txt|xargs -n 1 echo
    /bin
    tao
    shou
    kun
    root@localhost:~/test3#cat txt|xargs echo
    /bin tao shou kun
  • -t 表示先打印命令,然后再执行。

    1
    2
    3
    root@localhost:~/test#cat txt|xargs -t echo
    echo /bin tao shou kun
    /bin tao shou kun
  • -i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替。

    1
    > ls | xargs -t -i mv {} {}.bak
  • -r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。

    1
    2
    root@localhost:~/test#echo ""|xargs -t -r  mv
    root@localhost:~/test#
  • -s num 命令行的最大字符数,指的是xargs后面那个命令的最大命令行字符数

    1
    2
    3
    4
    5
    6
    7
    root@localhost:~/test#cat test |xargs -i -x  -s 14 echo "{}"
    exp1
    exp5
    file
    xargs: argument line too long
    linux-2
    root@localhost:~/test#
  • -L num Use at most max-lines nonblank input lines per command line.-s是含有空格的。

  • -l 同-L

  • -d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    root@localhost:~/test#cat txt |xargs -i -p echo {}
    echo /bin tao shou kun ?...y
    root@localhost:~/test#cat txt |xargs -i -p -d " " echo {}
    echo /bin ?...y
    echo tao ?.../bin
    y
    echo shou ?...tao
    再如:
    root@localhost:~/test#cat test |xargs -i -p -d " " echo {}
    echo exp1
    exp5
    file
    linux-2
    ngis_post
    tao
    test
    txt
    xen-3
    ?...y
    root@localhost:~/test#cat test |xargs -i -p echo {}
    echo exp1 ?...y
    echo exp5 ?...exp1
    y
    echo file ?...exp5
    y
  • -x exit的意思,主要是配合-s使用。

  • -P 修改最大的进程数,默认是1,为0时候为as many as it can