業務經理 _ 唐女士
QQ在綫客服

上班時間 (星期一至星期五)
10:30-12:00 14:00-17:30

軟件銷售 _ 卓先生
QQ在綫客服

上班時間 (星期一至星期五)
10:30-12:00 14:00-17:30

技術推廣 _ 梁先生
技術推廣 _ 江先生
QQ在綫客服

上班時間 (星期一至星期五)
09:00-12:00 14:00-18:30

基本功能

“xargs” 可實現 “將標準輸入,作為命令的參數”。

“xargs” 是一個強有力的命令,它能捕獲一個命令的輸齣,然後傳遞給另一個命令。

“xargs” 是給命令傳遞參數的一個過濾器,也是組閤多個命令的一個工具。

“xargs” 把一個數據流分割為一些足夠小的塊,以方便過濾器和命令進行處理。

通常情況下,“xargs” 會從管道或 stdin 中讀取數據;但是,“xargs” 也能從文件輸齣中讀取數據。

“xargs” 的默認命令是 echo,這意味著,通過管道傳遞給 “xargs” 的輸入將會包含換行和空白;不過, 通過 “xargs” 的處理,換行和空白會被空格取代。


作用:

“xargs” 可將多行輸入,轉換為單行 (特殊功效)。

“xargs” 可將輸入參數整理後,去除 <newline> 換行符,以一列錶形式進行處理。

使用 xargs -n 參數適當控製,可避免參數過長引發的問題;對於經常産生大量輸齣的命令,比如:find、locate 和 grep 來說非常有用。


譬如:

cho "--help" | xargs ca

輸齣內容如下:

Usage: cat [OPTION]... [FILE]...

Concatenate FILE(s), or standard input, to standard output.

&nb

A, --show-all                     equivalent to -vET

b, --number-nonblank    number nonempty output lines, overr

                                         equivalent to -vE

E, --show-ends               display $ at end of each

, --number                      number all outpu

, --squeeze-blank         suppress repeated empty outpu

                                          equivalent to -vT

T, --show-tabs                 display TAB characters as ^I

u                                         (ignored)

v, --show-nonprinting     use ^ and M- notation, except for LFD and TAB

     --help                             display this help a

     --version                        output version information a

&nb

With no FILE, or when FILE is -, read standard input.

&nb

Exa

cat f - g Output f's contents, then standard input, then g's contents.

cat Copy standard input to standard output.

&nb

Report cat bugs to bug-coreutils@gnu.org

GNU coreutils home page: <http://www.gnu.org/software/coreutils/&g

General help using GNU software: <http://www.gnu.org/gethelp/&g

For complete documentation, run: info coreutils 'cat invocation'


管道可實現 “將前麵的標準輸齣,作為後麵的標準輸入”。

譬如:

cho "--help" | ca

輸齣內容如下:

--help

基本語法

Usage: xargs [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]

[-E eof-str] [-e[eof-str]] [--eof[=eof-str]]

[-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]

[-I replace-str] [-i[replace-str]] [--replace[=replace-str]]

[-n max-args] [--max-args=max-args]

[-s max-chars] [--max-chars=max-chars]

[-P max-procs] [--max-procs=max-procs] [--show-limits]

[--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]

[--version] [--help] [command [initial-arguments]]


譬如:

command | xargs -item comma

不帶 command 時,默認使用 echo 輸齣

Option 參數

# 參數 EN 解釋 中文翻譯 備注
01 a file, --arg-file=fi Read items from file instead of standard input. If you use this option, stdin remains unchanged when commands are run. Otherwise, stdin is redirected from /dev/null. 從文件讀取項,而不是 stdin 標準輸入。若您使用此選項,stdin 標準輸入會保持不變,當運行命令時。否則,stdin 標準輸入會自 /dev/null 重定嚮。
02 0, Input items are terminated by a null character instead of by whitespace, and the quotes and back‐slash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode. 通過 null 字符終止輸入項,而不是空格;且引號、反-斜杠 (\) 並不特殊 (每個字符取字麵意思)。會禁用像對待任何其他自變量的文件字符串結尾。當輸入項可能包含空格、引號或反斜杠時,會很有用。GNU 發現 -print0 選項産生的輸入,適閤這種模式。
03 im,--delimiter=delim Input items are terminated by the specified character. Quotes and backslash are not special; every character in the input is taken literally. Disables the end-of-file string, which is treated like any other argument. This can be used when the input consists of simply newline-separated items, al‐though it is almost always better to design your program to use --null where this is possible. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. Octal and hexadecimal escape codes are understood as for the printf com‐mand. Multibyte characters are not supported.
04 E eof Set the end of file string to eof-str. If the end of file string occurs as a line of input, the rest of the input is ignored. If neither -E nor -e is used, no end of file string is used.
05 [eof-str], --eof[=eof-str] This option is a synonym for the -E option. Use -E instead, because it is POSIX compliant while this option is not. If eof-str is omitted, there is no end of file string. If neither -E nor -e is used, no end of file string is used.
06 p Print a summary of the options to xargs and exit. 打印 xargs 選項匯總並退齣。
07 I repla Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.
08 [replace-str], --replace[=replace-str] This option is a synonym for -Ireplace-str if replace-str is specified, and for -I{} otherwise. This option is deprecated; use -I instead.
09 L max-lin Use at most max-lines nonblank input lines per command line. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x.
10 [max-lines], --max-lines[=max-lines] Synonym for the -L option. Unlike -L, the max-lines argument is optional. If max-lines is not specified, it defaults to one. The -l option is deprecated since the POSIX standard specifies -L instead.
11 n max-args, --max-args=max-arg Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
12 p, --interactiv Prompt the user about whether to run each command line and read a line from the terminal. Only run the command line if the response starts with `y' or `Y'. Implies -t.
13 , --no-run-if-emp If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.
14 max-chars, --max-chars=max Use at most max-chars characters per command line, including the command and initial-arguments and the terminating nulls at the ends of the argument strings. The largest allowed value is system-de‐pendent, and is calculated as the argument length limit for exec, less the size of your environment, less 2048 bytes of headroom. If this value is more than 128KiB, 128Kib is used as the default val‐ue; otherwise, the default value is the maximum. 1KiB is 1024 bytes.
15 , --v Print the command line on the standard error output before executing it. 執行前,打印標準錯誤輸齣命令行。
16 v Print the version number of xargs and exit. 打印 xargs 版本號並退齣。
17 how-limi Display the limits on the command-line length which are imposed by the operating system, xargs' choice of buffer size and the -s option. Pipe the input from /dev/null (and perhaps specify --no-run-if-empty) if you don't want xargs to do anything.
18 x, --ex Exit if the size (see the -s option) is exceeded.
19 P max-procs, --max-procs=max-p Run up to max-procs processes at a time; the default is 1. If max-procs is 0, xargs will run as many processes as possible at a time. Use the -n option with -P; otherwise chances are that only one exec will be done.

詳細用法


功能 # 命令規範 文字解釋 示例 示例解釋
01 find /tmp -name core -type f -print | xargs /bin/rm -f ind files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines or spaces.
01 find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled.
01 find /tmp -depth -name core -type f Find files named core in or below the directory /tmp and delete them, but more efficiently than in the pre‐vious example (because we avoid the need to use fork(2) and exec(2) to launch rm and we don't need the ex‐tra xargs process).
01 f1 < /etc/passwd | sort | x Generates a compact listing of all the users on the system.
01 xargs sh -c 'emacs "$@" < /dev/tty' ema Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on xargs' standard input. This example achieves the same effect as BSD's -o option, but in a more flexible and portable way.
刪除文件 01 find / -name filename | xargs rm -rf 徹底清潔移除 filenam find / -name nginx | xargs rm -rf 徹底清潔移除 nginx
查看幫助 01 xargs --help 查看 xargs 摘要幫助信息
查看版本 01 xargs --v 查看 xargs 版本號、版本聲明、許可協議、作者等信息