xargs 可將標準輸入 (文件或之前命令的輸齣), 作為之後命令的輸入參數。
xargs 可將多行輸入, 轉換為單行。
xargs 可將輸入去除 <newline> 換行符後, 以列錶形式進行處理。
xargs -n 可避免參數過長引發異常, 如 find locate grep。
cho --help | xargs cat who --help | cat
基本語法
xargs [OPTION]... COMMAND [INITIAL-ARGS]...
注意:
長選項強製可選參數,對於短選項也是強製或可選的。
采用自變量 INITIAL-ARGS 運行 COMMAND, 並從輸入讀取更多自變量。
譬如: command | xargs -item command
不帶 command 時,默認使用 echo 輸齣。
OPTION 選項
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| -0, --null |
items are separated by a null, not whitespace; disables quote and backslash processing and logical EOF processing |
項由 null 分隔,非空格; 禁用引號 反斜杠處理及邏輯 EOF (文件結束) 處理 |
|
| -a, --arg-file=FILE | read arguments from FILE, not standard input | 從 FILE 讀取自變量,非標準輸入 | |
| -d, --delimiter=CHARACTER |
items in input stream are separated by CHARACTER, not by whitespace; disables quote and backslash processing and logical EOF processing |
輸入流中的項由 CHARACTER 分隔,非空格; 禁用引號 反斜杠處理及邏輯 EOF (文件結束) 處理 |
|
| -E END |
set logical EOF string; if END occurs as a line of input, the rest of the input is ignored (ignored if -0 or -d was specified) |
設置邏輯 EOF (文件結束) 字符串; 若 END 作為輸入行齣現, 忽略其餘輸入 (忽略,若 -0 或 -d 被指定) |
|
| -e, --eof[=END] |
equivalent to -E END if END is specified; otherwise, there is no end-of-file string |
相當於 -E END,若 END 被指定; 否則,沒有 EOF (文件結束) 字符串 |
|
| -I R | same as --replace=R | 如同 --replace=R | |
| -i, --replace[=R] |
replace R in INITIAL-ARGS with names read from standard input; if R is unspecified, assume {} |
采用從標準輸入讀取的名稱, 替換 INITIAL-ARGS 中的 R; 若 R 未指定,則假定 {} |
|
| -L, --max-lines=MAX-LINES | use at most MAX-LINES non-blank input lines per command line | 每命令行最多使用 MAX-LINES 非空白輸入行 | |
| -l[MAX-LINES] |
similar to -L; but defaults to at most one non-blank input line, if MAX-LINES is not specified |
類似 -L; 但默認最多一非空白輸入行, 若 MAX-LINES 未被指定 |
|
| -n, --max-args=MAX-ARGS | use at most MAX-ARGS arguments per command line | 每命令行最多使用 MAX-ARGS 自變量 | |
| -o, --open-tty |
Reopen stdin as /dev/tty in the child process, before executing the command; useful to run an interactive application. |
在子級進程中以 /dev/tty 重新打開 stdin (標準輸入), 在執行命令前; 對運行交互式應用程序會很有用。 |
|
| -P, --max-procs=MAX-PROCS | run at most MAX-PROCS processes at a time | 每次最多運行 MAX-PROCS 進程 | |
| -p, --interactive | prompt before running commands | 運行命令前提示 | |
| --process-slot-var=VAR | set environment variable VAR in child processes | 在子級進程中設置環境變量 VAR | |
| -r, --no-run-if-empty |
if there are no arguments, then do not run COMMAND; if this option is not given, COMMAND will be run at least once |
如果沒有自變量, 則不運行 COMMAND; 如果此選項不給定, COMMAND 將至少運行一次 |
|
| -s, --max-chars=MAX-CHARS | limit length of command line to MAX-CHARS | 把命令行長度限製為 MAX-CHARS | |
| --show-limits | show limits on command-line length | 展示命令行長度限製 | |
| -t, --verbose | print commands before executing them | 打印命令,在執行它們之前 | |
| -x, --exit | exit if the size (see -s) is exceeded | 退齣,若超齣大小 (見 -s) | |
| --help | display this help and exit | 顯示此幫助並退齣 | |
| --version | output version information and exit | 輸齣版本信息並退齣 |
| 功能 | 命令 | 文字解釋 | 示例 | 示例解釋 |
|---|---|---|---|---|
| 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. | |||
| 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. | |||
| 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). | |||
| f1 < /etc/passwd | sort | x | Generates a compact listing of all the users on the system. | |||
| 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. | |||
| 清零 | find / -name filename | xargs rm -rf | 徹底清潔移除 filename | find / -name nginx | xargs rm -rf | 徹底清潔移除 nginx |
| 幫助 | xargs --help | 展示幫助信息 | ||
| 版本 | xargs --version | 展示版本信息 |
版權聲明: 本文為獨傢原創稿件,版權歸 樂數軟件 ,未經許可不得轉載。