Listing 15. Example of using of the xargs tool.
清单15. xargs工具的使用示例。
Listing 14. Example of output from the xargs tool.
清单14.xargs工具产生的输出示例。
If such an error occurs, use a pipe and the xargs utility.
如果出现此类错误,可使用管道xargs实用工具。
The xargs command will not build arbitrarily long commands.
xargs命令不会构建任意长度的命令。
Using the find command with the -exec option or with xargs.
使用带有-exec选项或xargs的find命令。
In Listing 19 we try using find with -exec rather than ls with xargs.
在清单19中我们尝试使用带有-exec的find,而不是带有xargs的ls。
Next, awk reduces the output to the process ID, which you feed into xargs.
接下来,awk只显示输出结果中的进程ID,并将它传递给xargs。
Xargs reads data from a pipe and runs a specified command for every line read.
xargs命令可从管道中读取数据并为每行读取内容运行指定命令。
We could use xargs instead, but we already saw problems with blanks in file names.
我们也可以改为使用 xargs,但我们已经看到了文件名中包含空格时出现的问题。
Compared to what you already learned about using xargs, there are several differences.
与前面学习的xargs命令相比,它有几个不同之处。
Use the xargs tool as a filter for making good use of output culled from the find command.
使用xargs工具作为筛选器,以充分利用从find命令挑选的输出。
However, when xargs builds the command, it will pass as many parameters at once as it possibly can.
不过,当xargs构建命令时,它将一次传递尽可能多的参数。
Listing 16 shows one way you might pass a directory listing through xargs to a command such as grep.
清单16显示了一种通过xargs将目录清单传递到命令(比如grep)的方法。
By default, xargs delimits input with a space (or newline) to produce a list of files to operate on.
在缺省情况下,xargs将使用空格(或者换行符)对输入字符串进行划分,以便产生可以进行操作的一个文件列表。
The xargs command reads standard input and then builds and executes commands with the input as parameters.
xargs命令读取标准的输入,然后使用参数作为输入构建和执行命令。
For instance, the command pkill -9 -u strike ZSH is the equivalent of pgrep -u strike ZSH | xargs kill -9.
例如,命令pkill -9 -ustrike zsh与pgrep - ustrike z sh |xargs kill - 9等效。
Wherever the replacement string occurs in the command you ask xargs to execute, it will be replaced by an argument.
如果xargs将要执行的命令包含有替换字符串,那么将使用参数替换它。
If you run find on such a batch of files and pass the list of results to xargs, the Spaces in the file names cause errors.
如果您对大量的文件运行find命令,并且将结果列表传递给xargs,那么文件名中的空格将会导致错误。
Although our examples have used simple text files for illustration, you will seldom want to use xargs with input like this.
尽管我们的例子为了便于演示使用了简单的文本文件,您很少看到包含这样的输入的xargs。
Now we will look at the -exec option of find, which has similar functionality to using find and piping the output to xargs.
现在我们看一下find命令的- exec选项,其功能类似于使用find并通过管道将输出指向xargs。
xargs consumes the filenames from find and runs grep -l repeatedly to process every file, no matter how many files are named.
xargs 接收来自find 的文件名并重复运行grep-l以处理每个文件,而不论有多少个文件。
In Listing 17 we first copy text1 to "text 1" and then show some ways of using a list of file names containing blanks with xargs.
在清单17中,我们首先将text1复制到“text1”,然后显示一些在xargs命令中使用包含空格的文件名列表的方法。
A better solution, when available, is the -0 option of xargs, so that the null character (\ 0) is used to delimit input arguments.
另外一种更好的解决办法是使用xargs的-0选项,从而使用null字符串(\ 0)分隔输入参数。
For instance, xargs takes not records but names of files as input, and all of the actual processing is done by some other program.
例如,xargs不是接受记录而是接受文件名作为输入,并且所有的实际处理都是由其他程序完成的。
You can also use the -l option of xargs to have it treat lines as arguments rather than the default of individual blank-delimited tokens.
您还可以使用xargs的- l选项让命令将行当作参数看待,而不是默认的以单个空格分隔的标记。
To preserve an entire file name (remember, UNIX arguments are separated by white space), be sure to use find -print0 and its companion, xargs -0.
为了保持完整的文件名(请记住,UNIX参数以空格分隔),一定要使用find - print0和xargs - 0。
The xargs command then takes each process ID (since you used -l 1 to grab one line at a time), and USES sudo kill -hup to send each one a hangup signal.
然后,xargs命令接受每个进程ID(因为使用了- l1以便一次提取一行内容),并使用sudokill -HUP向相应的进程发送一个挂起信号。
If the input contains blanks that are protected by single or double quotes, or by backslash escaping, then xargs will not break the input at such points.
如果输入包含由单引号或双引号保护的空格,或使用了斜杠进行转义,那么xargs将不在遇到这些空格时中断。
The proper, portable technique is to use find -print0, combined with xargs -0, to delimit file names with the NUL character. Here's the favored approach.
一种适当的、可移植的技术是使用find - print0,加上xargs -0,以便使用NUL字符对文件名进行划分。
In the older Linux systems, or on UNIX systems that may still have the limitation, it is useful to know how to use xargs so you can handle such situations.
在更旧的Linux系统或UNIX系统上仍然存在该限制,因此了解如何使用xargs以处理这种问题非常有用。
应用推荐