For faster navigation, this Iframe is preloading the Wikiwand page for xargs.

xargs

xargs是一条Unix类Unix操作系统的常用命令。它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题[1]。xargs的作用一般等同于大多数Unix shell中的反引号,但更加灵活易用,并可以正确处理输入中有空格等特殊字符的情况。对于经常产生大量输出的命令如findlocategrep来说非常有用。

示例

[编辑]

例如,下面的命令:

rm $(find /path -type f)

如果path目录下文件过多就会因为“参数列表过长”而报错无法执行。但改用xargs以后,问题即获解决。

find /path -type f -print0 | xargs -0 rm

本例中xargs将find产生的长串文件列表拆散成多个子串,然后对每个子串调用rm。-print0表示输出以null分隔(-print使用换行);-0表示输入以null分隔。这样要比如下使用find命令效率高的多。

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

上面这条命令会对每个文件调用"rm"命令。当然使用新版的"find"也可以得到和"xargs"命令同样的效果:

find /path -type f -exec rm '{}' +
find . -name "*.foo" | xargs grep bar

该命令大体等价于

grep bar $(find . -name "*.foo")
find . -name "*.foo" -print0 | xargs -0 grep bar

使用了GNU特殊规定的空字符。

find . -name "*.foo" -print0 | xargs -0 -t -r vi

与上面的基本相同但启动vi进行编辑。-t参数会提前打印错误资讯。-r参数是一个GNU扩展,表明在无输入情况下则不构造命令执行。

find . -name "*.foo" -print0 | xargs -0 -i mv {} /tmp/trash

使用-i参数将{}中内容替换为列表中的内容。

参见

[编辑]

参考

[编辑]
  1. ^ GNU Core Utilities FAQ. [2008-03-12]. (原始内容存档于2020-11-11). 

外部链接

[编辑]

手册页

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
xargs
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?