zoukankan      html  css  js  c++  java
  • 一天一个 Linux 命令(22):xargs 命令

    一、简介

    xargs英文全拼: extended arguments

    xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。

    xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。

    xargs 也可以将单行或多行文本输入转换为其他格式,例如多行变单行,单行变多行。

    xargs 默认的命令是 echo,这意味着通过管道传递给 xargs 的输入将会包含换行和空白,不过通过 xargs 的处理,换行和空白将被空格取代。

    xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令。

    简单来说,xargs 的作用是给其他命令传递参数,是构建单行命令的重要组件之一。

    之所以会有这个命令,主要是因为很多命令不支持|管道来传递参数,而日常工作中却有这个必要,所以就有了 xargs 命令。

    二、格式说明

    xargs [OPTION]... COMMAND INITIAL-ARGS...
    
    Mandatory arguments to long options are mandatory for short options too.
    Non-mandatory arguments are indicated by [square brackets]
      -0, --null                   Items are separated by a null, not whitespace.
                                   Disables quote and backslash processing
      -a, --arg-file=FILE          Read arguments from FILE, not standard input
      -d, --delimiter=CHARACTER    Input items are separated by CHARACTER, not by
                                   blank space. Disables quote and backslash
                                   processing
      -E END                       If END occurs as a line of input, the rest of
                                   the input is ignored.
      -e [END], --eof[=END]        Equivalent to -E END if END is specified.
                                   Otherwise, there is no end-of-file string
      --help                       Print a summary of the options to xargs.
      -I R                         same as --replace=R (R must be specified)
      -i,--replace=[R]             Replace R in initial arguments with names
                                   read from standard input. If R is
                                   unspecified, assume {}
      -L,-l, --max-lines=MAX-LINES Use at most MAX-LINES nonblank input lines per
                                   command line
      -l                           Use at most one nonblank input line per
                                   command line
      -n, --max-args=MAX-ARGS      Use at most MAX-ARGS arguments per command
                                   line
      -P, --max-procs=MAX-PROCS    Run up to max-procs processes at a time
      -p, --interactive            Prompt before running commands
      --process-slot-var=VAR       Set environment variable VAR in child
                                   processes
      -r, --no-run-if-empty        If there are no arguments, run no command.
                                   If this option is not given, COMMAND will be
                                   run at least once.
      -s, --max-chars=MAX-CHARS    Limit commands to MAX-CHARS at most
      --show-limits                Show limits on command-line length.
      -t, --verbose                Print commands before executing them
      --version                    Print the version number
      -x, --exit                   Exit if the size (see -s) is exceeded
    

    注意,xargs一般不会单独使用, 一般都是是和管道符|一起使用的。

    command |xargs -item  command
    

    三、选项说明

    -0, --null
     如果输入的 stdin 含有特殊字符,例如反引号 `、反斜杠 、空格等字符时,xargs 将它还原成一般字符。为默认选项
    -a, --arg-file=FILE
     从指定的文件 FILE 中读取输入内容而不是从标准输入
    -d, --delimiter=DEL
     指定 xargs 处理输入内容时的分隔符。xargs 处理输入内容默认是按空格和换行符作为分隔符,输出 arguments 时按空格分隔
    -E EOF_STR
     EOF_STR 是 end of file string,表示输入的结束
    -e, --eof[=EOF_STR]
     作用等同于 -E 选项,与 -E 选项不同时,该选项不符合 POSIX 标准且 EOF_STR 不是强制的。如果没有 EOF_STR 则表示输入没有结束符
    -I REPLACE_STR
     将 xargs 输出的每一项参数单独赋值给后面的命令,参数需要用指定的替代字符串 REPLACE_STR 代替。REPLACE_STR 可以使用 {} $ @ 等符号,其主要作用是当 xargs command 后有多个参数时,调整参数位置。例如备份以 txt 为后缀的文件:find . -name "*.txt" | xargs -I {}  cp {} /tmp/{}.bak
    -i, --replace[=REPLACE_STR]
     作用同 -I 选项,参数 REPLACE_STR 是可选的,缺省为 {}。建议使用 -I 选项,因为其符合 POSIX
    -L MAX_LINES
     限定最大输入行数。隐含了 -x 选项
    -l, --max-lines[=MAX_LINES]
     作用同 -L 选项,参数 MAX_LINES 是可选的,缺省为 1。建议使用 -L 选项,因为其符合 POSIX 标准
    -n, --max-args=MAX_ARGS
     表示命令在执行的时候一次使用参数的最大个数
    -o, --open-tty
     在执行命令之前,在子进程中重新打开stdin作为/dev/TTY。如果您希望xargs运行交互式应用程序,这是非常有用的
    -P, --max-procs=MAX_PROCS
     每次运行最大进程;缺省值为 1。如果 MAX_PROCS 为 0,xargs 将一次运行尽可能多的进程。一般和 -n 或 -L 选项一起使用
    -p, --interactive
     当每次执行一个 argument 的时候询问一次用户
    --process-slot-var=NAME
     将指定的环境变量设置为每个正在运行的子进程中的唯一值。一旦子进程退出,将重用该值。例如,这可以用于初始负荷分配方案
    -r, --no-run-if-empty
     当 xargs 的输入为空的时候则停止 xargs,不用再去执行后面的命令了。为默认选项
    -s, --max-chars=MAX_CHARS
     命令行的最大字符数,指的是 xargs 后面那个命令的最大命令行字符数,包括命令、空格和换行符。每个参数单独传入 xargs 后面的命令
    --show-limits
     显示操作系统对命令行长度的限制
    -t, --verbose
     先打印命令到标准错误输出,然后再执行
    --help
     显示帮助信息并退出
    --version
     显示版本信息并退出
    -x, --exit
     配合 -s 使用,当命令行字符数大于 -s 指定的数值时,退出 xargs
    

    注意,长选项的强制性参数对于短选项也是强制的。

    四、命令功能

    结合其他命令,将标准输入转为命令行参数

    五、常见用法

    1.将管道左侧的标准输入,转为命令行参数hello world,传给第二个echo命令

    # echo "hello world"|xargs echo
    hello world
    

    2.将shell 的特殊字符反引号还原为一般字符

    # echo '`hello` world'|xargs echo
    `hello` world
    

    如果直接执行以下操作,会报无法找到命令 hello的错误,因为反引号在shell中会将hello作为一个命令来执行,但是 hello不是一个命令

    # echo `hello` world
    -bash: hello: command not found
    world
    

    如果使用-t参数,则表示先打印命令,然后再执行

    # echo '`hello` world'|xargs -t echo
    echo `hello` world 
    `hello` world
    

    3.从指定的文件中读取输入内容,然后重新格式化后输出

    首先,先定义一个测试文件test.txt

    # cat test.txt 
    hello
    i
    love
    China
    ,
    my
    name
    is
    joshua317
    

    多行输入单行输出

    # cat test.txt |xargs
    hello i love China , my name is joshua317
    

    -n 选项多行输出

    # cat test.txt |xargs -n5
    hello i love China ,
    my name is joshua317
    

    -d 选项可以自定义一个分隔符

    # cat test.txt |xargs -d","
    hello
    i
    love
    China
     
    my
    name
    is
    joshua317
    

    默认情况下,xargs将换行符和空格作为分隔符,把标准输入分解成一个个命令行参数。

    4.从指定的文件中读取输入内容而不是从标准输入,然后执行命令

    首先,先定义一个文件ip.txt

    # cat ip.txt 
    www.baidu.com
    114.114.114.114
    www.qq.com
    

    xargs命令使用-a选项,后跟文件名,从文件读取内容,使用-L 1选项,该选项表示xargs一次读取一行。如果省略此选项,xargs将把所有ip传递给一个ping命令。

    # xargs -a ip.txt -t -L 1 ping -c 1
    ping -c 1 www.baidu.com 
    PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
    64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=251 time=10.6 ms
    
    --- www.a.shifen.com ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 10.636/10.636/10.636/0.000 ms
    ping -c 1 114.114.114.114 
    PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
    64 bytes from 114.114.114.114: icmp_seq=1 ttl=251 time=33.3 ms
    
    --- 114.114.114.114 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 33.327/33.327/33.327/0.000 ms
    ping -c 1 www.qq.com 
    PING ins-r23tsuuf.ias.tencent-cloud.net (175.27.8.138) 56(84) bytes of data.
    64 bytes from 175.27.8.138 (175.27.8.138): icmp_seq=1 ttl=56 time=4.42 ms
    
    --- ins-r23tsuuf.ias.tencent-cloud.net ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 4.426/4.426/4.426/0.000 ms
    

    与wget 结合,下载文件内的所有链接

    假设有一个文件包含了很多你希望下载的 URL,你能够使用 xargs下载所有链接

    # cat url-list.txt | xargs wget -c
    

    5.使用管道符|传输到xargs,并为每个参数运行touch命令

    # echo "file1 file2 file3"|xargs -t touch
    touch file1 file2 file3 
    

    如果执行前需要询问是否执行,则使用-p参数

    # echo "file1 file2 file3"|xargs -t -p touch
    touch file1 file2 file3 ?...
    

    如果需要xargs多次运行指定的命令,则使用-n参数,指定要传递给命令的参数个数,认情况下,传递给命令的参数数量由系统限制决定。

    # echo "file1 file2 file3"|xargs -t -p -n1 touch
    touch file1 ?...y
    touch file2 ?...y
    touch file3 ?...y
    

    6.如何在xargs后面运行多项命令

    要使用xargs运行多个命令,使用-i或者-I选项。在-i或者-I后面自定义一个传递参数符号,如%,所有匹配的项都会替换为传递给xargs的参数。

    # 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 Oct  8 10:53 file1
    -rw-r--r-- 1 root root 0 Oct  8 10:53 file2
    -rw-r--r-- 1 root root 0 Oct  8 10:53 file3
    

    删除上面创建的文件

    # echo "file1 file2 file3"|xargs -t -I % sh -c 'ls -l %;rm %'
    sh -c ls -l file1 file2 file3;rm file1 file2 file3 
    -rw-r--r-- 1 root root 0 Oct  8 10:53 file1
    -rw-r--r-- 1 root root 0 Oct  8 10:53 file2
    -rw-r--r-- 1 root root 0 Oct  8 10:53 file3
    

    复制当前目录下的所有log文件到 /data/logs目录下

    xargs 的一个选项 -I,使用 -I 指定一个替换字符串%,这个字符串在 xargs 扩展时会被替换掉,当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次

    ls *.log | xargs -n1 -I % cp % /data/logs
    

    7.xargs与find一起使用

    用 rm 删除太多的文件时候,可能得到一个错误信息:/bin/rm Argument list too long.,"参数列表过长",而无法执行,用 xargs 去避免这个问题

    find /data/log -type f -name "*.log" -print0 | xargs -0 rm -f
    

    查找所有的 log 文件,并且压缩它们:

    find . -type f -name "*.log" -print0 | xargs -0 tar -czvf log.tar.gz
    

    注意:

    由于xargs默认将空格作为分隔符,所以不太适合处理文件名,因为文件名可能包含空格。

    find命令有一个特别的参数-print0,指定输出的文件列表以null分隔。然后,xargs命令的-0参数表示用null当作分隔符。所以要避免包含换行符或其他特殊字符的文件名出现问题,请始终使用find的-print0选项,这样可以使find打印完整的文件名,配合xargs命令使用-0或者--null选项可以正确的执行。

    查找文件里面包含的字符

    找出当前目录下所有 log文件以后,对每个文件搜索一次是否包含字符串joshua317

    # find . -name "*.log" | xargs grep "joshua317"
    

    从根目录开始查找所有扩展名为.log的文本文件,并找出包含"ERROR"的行

    find / -type f -name "*.log" | xargs grep "ERROR"
    

    8.批量杀掉多个php进程

    ps -ef|grep php|grep -v grep|cut -c 9-15|xargs kill -9
    

    持续整理。。。

     

     
  • 相关阅读:
    Windows SDK 之 mciSendString最后一个参数
    java常用包下载地址(非maven)
    windows api(GDI)实现图片旋转
    windows sdk版本 之 并查集生成迷宫
    自签https证书2(适配新版chrome,不会显示“不安全”)
    数据结构——栈(Stacks)
    数据结构——表(list)
    数据结构——链表(linkedlist)
    解题报告1010 诡秘的余数
    函数体中用指针返回数组的方法
  • 原文地址:https://www.cnblogs.com/joshua317/p/15381015.html
Copyright © 2011-2022 走看看