zoukankan      html  css  js  c++  java
  • xargs命令

    xargs 又称管道命令。它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题。

    主要参数

    -i 用 {} 代替 传递的数据
    -I string 用string来代替传递的数据
    -n[数字] 设置每次传递几行数据
    -d 按标记切割字符串
    -t 显示执行详情
    -p 交互模式
    -P n 允许的最大线程数量为n
    -s[大小] 设置传递参数的最大字节数(小于131072字节)
    -x 大于 -s 设置的最大长度结束 xargs命令执行

    xargs和find:

    这条命令
    rm `find /path -type f`
    如果path目录下文件过多就会因为“参数列表过长”而报错无法执行。但改用xargs以后,问题即获解决。
    find /path -type f -print0 | xargs -0 rm

    本例中xargs将find产生的长串文件列表拆散成多个子串,然后对每个子串调用rm。

    -print0表示输出以null分隔,就是文件都紧挨着啦。

    -print使用换行,默认就是这个啦。

    -0表示输入以null分隔接收的参数,这个是为了配合find的-print啦,默认是空格啦,这样防止文件名中有空格的文件造成xargs识别错误啦。

    这样要比如下使用find命令效率高的多。

    find /path -type f -exec rm '{}' ;

    -i参数:
    [root@localhost ~]# ls |grep .php |xargs -i mv {} {}.bak     #将当前目录下php文件,改名字
    
    [root@localhost ~]# find ./ -name "*.tmp" | xargs -i rm -rf {}  #删除当前文件夹下的tmp文

     

    -n 参数:

    [root@localhost ~]# cat example.txt
    1 2 3 4 5
    6 7
    8
    [root@localhost ~]# cat example.txt | xargs #将数据转为一行输出,xargs默认用空格分隔每次传递的数据

    1 2 3 4 5 6 7 8
    [root@localhost ~]# cat example.txt | xargs -n 2 #按照每行两列输出 1 2 3 4 5 6 7 8
    [root@localhost ~]# echo "splitXhiXamosliXsplit" | xargs -d "X" -n 1 #用-d指定分隔符分隔字符串
    split
    hi
    amosli
    split

     

    读取stdin,将格式化参数传递给命令:

    [root@localhost ~]# cat cecho.sh
    echo $*'#'
    
    #需求1:输出多个参数
    [root@localhost ~]# sh cecho.sh arg1
    arg1#
    [root@localhost ~]# sh cecho.sh arg2
    arg2#
    [root@localhost ~]# sh cecho.sh arg3
    arg3#
    
    #需求2:一次性提供所有的命令参数
    [root@localhost ~]# sh cecho.sh arg1 arg2 arg3
    arg1 arg1 arg2 arg3#
    
    #针对需求1、2,使用xargs代替,先用vi建一个新文件args.txt,如下:
    [root@localhost ~]# cat args.txt
    arg1
    arg2
    arg3
    #批量输出参数:
    [root@localhost ~]# cat args.txt | xargs -n 1
    arg1
    arg2
    arg3
    [root@localhost ~]# cat args.txt | xargs -n 2 sh cecho.sh
    arg1 arg2#
    arg3#
    #一次性输出所有参数:
    [root@localhost ~]# cat args.txt | xargs sh cecho.sh ;
    arg1 arg2 arg3#
  • 相关阅读:
    js布尔型
    C#函数(十一)
    JavaScript+jQuery从小工到专家学习笔记数值
    C#函数(十)
    C#函数(九)
    jquery 操作复选框 下拉框
    jquery select操作大全
    Jquery与Json实现Ajax
    如何打造创业团队
    jquery select操作大全
  • 原文地址:https://www.cnblogs.com/leezhxing/p/3958594.html
Copyright © 2011-2022 走看看