• Xargs用法详解(转)


    Xargs用法详解

    1. 简介

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

    find /sbin -perm +700 |ls -l       这个命令是错误的

    find /sbin -perm +700 |xargs ls -l   这样才是正确的

    xargs 可以读入 stdin 的资料,并且以空白字元或断行字元作为分辨,将 stdin 的资料分隔成为 arguments 。 因为是以空白字元作为分隔,所以,如果有一些档名或者是其他意义的名词内含有空白字元的时候, xargs 可能就会误判了~他的用法其实也还满简单的!就来看一看先!

    2. 选项解释

    淘宝、天猫、聚划算,商家短期打造爆款,秒杀、咚咚抢、优惠券,应有尽有,只有您想不到,没有做不到,开心购呀购(网址:www.kxgba.com,微信小程序搜索:开心购呀购),您的购物好助手!

    -0 当sdtin含有特殊字元时候,将其当成一般字符,想/'空格等

    例如:root@localhost:~/test#echo "//"|xargs  echo 

          root@localhost:~/test#echo "//"|xargs -0 echo 

           /

    -a file 从文件中读入作为sdtin,(看例一)

    -e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。(例二)

    -p 当每次执行一个argument的时候询问一次用户。(例三)

    -n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。(例四)

    -t 表示先打印命令,然后再执行。(例五)

    -i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替。(例六)

    -r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。(例七)

    -s num 命令行的最好字符数,指的是xargs后面那个命令的最大命令行字符数。(例八)

     

    -L  num Use at most max-lines nonblank input lines per command line.-s是含有空格的。

    -l  同-L

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

    -x exit的意思,主要是配合-s使用。

    -P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。

    3. 应用举例

    例一:

    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#

    例二:

    root@localhost:~/test#cat txt

    /bin tao shou kun

    root@localhost:~/test#cat txt|xargs -E 'shou' echo

    /bin tao

    root@localhost:~/test#

    例三:

    root@localhost:~/test#cat txt|xargs -p echo

    echo /bin tao shou kun ff ?...y

    /bin tao shou kun ff

    例四:

    root@localhost:~/test#cat txt|xargs -n1 echo

    /bin

    tao

    shou

    kun

    root@localhost:~/test3#cat txt|xargs  echo

    /bin tao shou kun

    例五:

    root@localhost:~/test#cat txt|xargs -t echo

    echo /bin tao shou kun 

    /bin tao shou kun

    例六:

    $ ls | xargs -t -i mv {} {}.bak

    例七:

    root@localhost:~/test#echo ""|xargs -t mv

    mv 

    mv: missing file operand

    Try `mv --help' for more information.

    root@localhost:~/test#echo ""|xargs -t -r  mv

    root@localhost:~/test#

    (直接退出)

    例八:

    root@localhost:~/test#cat test |xargs -i -x  -s 14 echo "{}"

    exp1

    exp5

    file

    xargs: argument line too long

    linux-2

    root@localhost:~/test#

    例九:

    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

     

    args的-i参数

    args与find经常结合来进行文件操作,平时删日志的时候只是习惯的去删除,比如
     # find . -type f -name "*.log" | xargs rm -rf *
    就将以log结尾的文件删除了,如果我想去移动或者复制就需要使用参数来代替了。
     
    xargs  -i 参数或者-I参数配合{}即可进行文件的操作。
     
    -I replace-str
                  Replace  occurrences  of  replace-str  in the initial-arguments with names read from standard input.  Also, unquoted blanks do not terminate
                  input items; instead the separator is the newline character.  Implies -x and -L 1.

           --replace[=replace-str], -i[replace-str]
                  This option is a synonym for -Ireplace-str if replace-str is specified, and for -I{} otherwise.  This option is deprecated; use -I  instead.
    man了一下看的还是不太懂,通过例子,做作实验将我的理解写一下。
     
    ############### 操作的目录下的文件###############
    [root@test05 ab]# ls
    1kk.zip  3kk.zip  5kk.zip  b.rar  d.rar  f.rar  h.rar  j.rar  mini.txt  ni.txt
    2kk.zip  4kk.zip  a.rar    c.rar  e.rar  g.rar  i.rar  k.rar  nii.txt
    ###################使用 i 参数 ##################
    [root@test05 ab]# find . -type f -name "*.txt" | xargs -i cp {}  /tmp/k/
    [root@test05 ab]# ls ../k/
    mini.txt  nii.txt  ni.txt
    [root@test05 ab]#
    ###################  使用 I  参数 ################
    [root@test05 ab]# find . -type f -name "*.txt" | xargs -I {} cp {}  /tmp/n/
    [root@test05 ab]# ls ../n/
    mini.txt  nii.txt  ni.txt
     
    结果出来了,
      加-i 参数直接用 {}就能代替管道之前的标准输出的内容;

     加 -I 参数 需要事先指定替换字符

  • 相关阅读:
    mongodb备份与恢复
    MongoDB-3.4安装文档
    (转)Zabbix 3.2.7编译安装记录
    (转)error while loading shared libraries:libmysqlclient.so.18 错误
    (转)如何使用Journalctl查看并操作Systemd日志
    (转)基于CentOS 7安装Zabbix 3.4和Zabbix4.0
    (转)yum安装MariaDB(使用国内镜像快速安装,三分钟安装完毕)
    (转)nmon和nmon analyser的下载和使用
    (转)Db2 数据库常见堵塞问题分析和处理
    (转)我是一个线程
  • 原文地址:https://www.cnblogs.com/woxingwoxue/p/4481373.html
Copyright © 2020-2023  润新知