久久青草精品A片狠狠,日韩欧美视频一区二区,亚洲国码AV日韩,国产精品黄在

Linux Xargs 命令的10個實(shí)用示例

2024-10-29 17:10:02 5309

Linux Xargs 命令的10個實(shí)用示例

當(dāng)我們將 xargs 命令與其他命令結(jié)合使用時,它非常有用。

本文通過幾個簡單的示例解釋了 xargs 命令的用法。

這些示例將幫助您了解 xargs 命令如何工作的基礎(chǔ)知識。但是,一旦你理解了這些概念,你就可以想出自己的聰明的 xargs 例子來解決各種命令行問題。

1. Xargs 基本示例

xargs 命令(默認(rèn)情況下)需要來自標(biāo)準(zhǔn)輸入的輸入,并在輸入上執(zhí)行 /bin/echo 命令。以下是在沒有任何參數(shù)的情況下執(zhí)行 xargs 或在不與任何其他命令組合的情況下執(zhí)行它時會發(fā)生的情況。

當(dāng)您鍵入不帶任何參數(shù)的 xargs 時,它會提示您通過 stdin 輸入輸入:

$ xargs

Hi,

Welcome to TGS.

鍵入內(nèi)容后,按 ctrl+d,這將在標(biāo)準(zhǔn)輸出上將字符串回顯給您,如下所示。

$ xargs

Hi,

Welcome to TGS.Hi, Welcome to TGS.

2. 使用 -d 選項(xiàng)指定分隔符

可以應(yīng)用分隔符,以便使用 xargs 中的 -d 選項(xiàng)按字面意思獲取輸入中的每個字符。

在前面的示例中,即使輸入在 ‘Hi’ 之后包含一個 \\n(換行符),但回顯的輸出不包含換行符 ‘\\n’ 。因此,前面示例中的輸出被合并為一行。

在以下示例中,當(dāng)您使用 -d\\n 時,它將在輸出中保留換行符分隔符,并完全按照輸入的方式顯示輸出。

$ xargs -d\\n

Hi,

Welcome to TGS.

鍵入上述內(nèi)容后,按 ctrl+d,這將在 stdout 上將字符串回顯給您,如下所示。但是,這一次它將保留換行符。

$ xargs -d\\n

Hi,

Welcome to TGS.Hi,

Welcome to TGS.

3. 使用 -n 選項(xiàng)限制每行輸出

默認(rèn)情況下,如前所述,xargs 顯示其標(biāo)準(zhǔn)輸入的任何內(nèi)容,如下所示。

$ echo a b c d e f| xargs

a b c d e f

但是,可以使用 -n 選項(xiàng)將 xargs 命令的輸出拆分為多行。

在以下示例中,我們使用了 -n 3,它將在 xargs 輸出中每行僅顯示 3 個項(xiàng)目。

$ echo a b c d e f| xargs -n 3

a b c

d e f

同樣,您還可以使用 -n 2 將輸出拆分為每行 2 個項(xiàng)目,如下所示。

$ echo a b c d e f| xargs -n 2

a b

c d

e f

4. 使用 -p 選項(xiàng)在執(zhí)行前提示用戶

使用選項(xiàng) -p,您可以確認(rèn)用戶執(zhí)行 xargs 命令。

考慮前面的示例,如果我們想確認(rèn)用戶每次執(zhí)行 /bin/echo 命令,請使用 -p 選項(xiàng)和 -n 選項(xiàng),如下所示。

$ echo a b c d e f| xargs -p -n 3

/bin/echo a b c ?...y

/bin/echo d e f ?...a b c

y

d e f

在下面的輸出中,我對所有 echo 輸出說“n”。因此, xargs 沒有執(zhí)行任何操作。

$echo abcdef| xargs -p -n

/bin/echo abc ?...n

/bin/echo def ?...n

/bin/echo ?...n

注意:當(dāng)您將 xargs 與諸如 rm 之類的破壞性命令結(jié)合使用時,這很有幫助。在這些情況下,您可能想看看 xargs 做了什么。

5. 使用 -r 選項(xiàng)避免空白輸入的默認(rèn) /bin/echo

當(dāng)有空白輸入時(即 xargs 命令沒有輸入),它將執(zhí)行 /bin/echo 命令,該命令將顯示一個新行,如下所示。

$ xargs -p

輸入“xargs -p”后按 ctrl-d,這將表明它執(zhí)行了 /bin/echo,如下所示。

$ xargs -p

                      /bin/echo ?...y

 

$

6. 使用 -t 選項(xiàng)打印命令和輸出

在以下示例中,鍵入“abcd”作為 xargs -t 命令的輸入。

$ xargs -t

A B C D

按 ctrl-d 完成上面的 xargs -t 命令,在顯示輸出之前會顯示 xargs 真正執(zhí)行的命令。在這種情況下,xargs 執(zhí)行的命令是“/bin/echo abcd”,此處顯示。

$ xargs -t

A B C D

/bin/echo abcd

A B C D

7. 結(jié)合 Xargs 和 Find 命令

它是 xargs 命令最重要的用法之一。當(dāng)您需要查找某種類型的文件并對它們執(zhí)行某些操作時(最流行的是刪除操作)。

當(dāng)我們與其他命令結(jié)合使用時,xargs 命令非常有效。

在以下示例中,我們獲取find 命令的輸出,并將其作為輸入傳遞給 xargs 命令。但是,我們不是執(zhí)行默認(rèn)的 /bin/echo 命令,而是指示 xargs 命令對輸入執(zhí)行 rm -rm 命令。

因此,在本例中,find 命令的輸出是所有帶有 *.c 擴(kuò)展名的文件,作為 xargs 命令的輸入,該命令反過來對所有 *.c 文件執(zhí)行“rm -rf”命令.

$ ls

one.c  one.h  two.c  two.h

 

$ find . -name "*.c" | xargs rm -rf

 

$ ls

one.h  two.h

8.刪除文件名中有空格的文件

所以我們看到,盡管對該目錄中的 .c 文件運(yùn)行了 rm 命令,但文件“The Geek Stuff.c”并沒有被刪除。這是因?yàn)榇宋募拿Q中包含空格字符。

如以下示例所示,它刪除了除一個之外的所有擴(kuò)展名為 *.c 的文件。即文件名中有空格的文件(即“The Geek Stuff.c”)沒有被rm 命令刪除。

$ touch "The Geek Stuff.c"

 

$ ls

one.c  one.h  two.c  two.h The Geek Stuff.c

 

$ find . -name "*.c" | xargs rm -rf

 

$ ls

one.h  two.h  The Geek Stuff.c

在這種情況下,使用 find 命令的 -print0 選項(xiàng)和 xargs 命令的 -0 選項(xiàng)來刪除文件,包括文件名中有空格的文件,如下所示。

$ ls

one.c  one.h  two.c  two.h The Geek Stuff.c

 

$ find . -name "*.c" -print0 | xargs -0 rm -rf

 

$ ls

one.h  two.h

9. 使用 –show-limits 選項(xiàng)在 xargs 上顯示系統(tǒng)限制

請參見下面的示例: 以下示例顯示了操作系統(tǒng)設(shè)置的所有限制,這些限制將對 xargs 命令的工作方式產(chǎn)生影響。

$ xargs --show-limits

Your environment variables take up 1203 bytes

POSIX upper limit on argument length (this system): 2093901

POSIX smallest allowable upper limit on argument length (all systems): 4096

Maximum length of command we could actually use: 2092698

Size of command buffer we are actually using: 131072

 

Execution of xargs will continue now, and it will try to read its input and

run commands; if this is not what you wanted to happen, please type the

end-of-file keystroke.

 

Warning: /bin/echo will be run at least once.  If you do not want that to happen,

then press the interrupt keystroke

10. 結(jié)合 Xargs 和 Grep 命令

xargs 命令可以與grep 命令結(jié)合使用,以從 find 命令的搜索結(jié)果中過濾特定文件。

在以下示例中,find 命令提供了所有 .c 文件作為 xargs 的輸入。

xargs 命令執(zhí)行 grep 命令以查找包含字符串“stdlib.h”的所有文件(在 find 命令提供的文件中)。

$ find . -name '*.c' | xargs grep 'stdlib.h'

./tgsthreads.c:#include

./valgrind.c:#include

./direntry.c:#include

./xvirus.c:#include

./temp.c:#include

 


提交成功!非常感謝您的反饋,我們會繼續(xù)努力做到更好!

這條文檔是否有幫助解決問題?

非常抱歉未能幫助到您。為了給您提供更好的服務(wù),我們很需要您進(jìn)一步的反饋信息:

在文檔使用中是否遇到以下問題: