参数代换:xargs

13 篇文章 0 订阅

1. xargs


args是arguments 参数的意思。
xargs 的作用就是产生某个命令参数的意思。
xargs可以读入stdin 数据,以空格或断行分隔,将stdin 分隔成 arguments。

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

对于不支持管道的命令,可以通过xargs提供该命令的引用stdin来使用。

2. xargs用法


xargs [-0epn] command
-0 : 如果输入的stdin包含特殊符号,如 `, \, 空格等字符,这个参数可以将它还原成一般字符。
-e : 是EOF(end of file) 的意思,后面可以接字符串,当xargs解析到这个字符串时,停止继续工作。
-p : 在执行每个命令参数时,都会询问用户意思
-n : 后面接次数,每次command命令执行时,要使用几个参数的意思。

3. xargs返回值


0 if it succeeds
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found
1 if some other error occurred.

4. 使用


将find到的数据,通过file检测文件类型。
$ find . * |  file     
Usage: file [-bcikLnNsvz] [-f namefile] [-F separator] [-m magicfiles] file...
       file -C -m magicfiles
Try `file --help' for more information.
通过xargs
$ find . * | xargs file 
.:                                                                                     directory
./test:                                                                              directory
./ui.cpp:                                                                                      ASCII C program text
文件参数过长例
rm `find /path -type f`
如果path目录下文件过多就会因为“参数列表过长”而报错无法执行。但改用xargs以后,问题即获解决
find /path -type f -print0 | xargs -0 rm

grep命令
grep bar `find . -name "*.foo"`
xargs可以使用下面方式
grep bar `find . -name "*.foo"`
find . -name "*.foo" -print0 | xargs -0 grep bar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值