自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Linux中使用xargs命令

系統(tǒng) Linux
我們經(jīng)常會(huì)看到 xargs 命令與 find 命令一起使用。find 命令提供文件名列表,xargs 命令可以讓我們逐個(gè)使用這些文件名,把它們當(dāng)作是另一個(gè)命令的輸入一樣使用。

什么是 xargs 命令

xargs命令從標(biāo)準(zhǔn)輸入或另一個(gè)命令的輸出中讀取文本行,并將其轉(zhuǎn)換為命令并執(zhí)行。

我們經(jīng)常會(huì)看到 xargs 命令與 find 命令一起使用。find 命令提供文件名列表,xargs 命令可以讓我們逐個(gè)使用這些文件名,把它們當(dāng)作是另一個(gè)命令的輸入一樣使用。

由于 rargs 會(huì)處理重定向,所以你需要提前了解關(guān)于標(biāo)準(zhǔn)輸入、輸出以及管道重定向相關(guān)的知識(shí)。關(guān)于管道重定向,可以參考我們之前的文章。

怎樣使用 xargs 命令

xargs 命令的語法如下:

xargs [options] [command [initial-arguments]]

但一般我們不這樣用,它的一個(gè)重要功能是將一個(gè)命令的輸出組合到另一個(gè)命令中。我們看一個(gè)例子:

假如在當(dāng)前路徑下有一些txt文件,以各種鮮花名稱命名,然后還有一個(gè)flowers.txt,記錄了所有這些txt文件的名稱:

[gliu@fedora work]$ ls
flowers.txt lily.txt one_lotus.txt rose.txt three_lotus.txt two_lotus.txt
[gliu@fedora work]$ cat flowers.txt
lily.txt
one_lotus.txt
rose.txt
three_lotus.txt
two_lotus.txt

現(xiàn)在我們的目標(biāo)是查看 flowers.txt 中提到的所有文件的文件大小。根據(jù)以往的經(jīng)驗(yàn),我們可以使用 cat 命令來顯示所有文件名,然后通過管道將其傳輸?shù)?du 命令來檢查文件大小。

但是如果我們直接使用管道的話,它不會(huì)給出 flowers.txt 中提到的每個(gè)文件的大?。?/span>

[gliu@fedora work]$ du -h
52K.
[gliu@fedora work]$ cat flowers.txt | du -h
52K.

為什么呢?首先,du 命令不接受標(biāo)準(zhǔn)輸入;其次,cat 命令的輸出不是單個(gè)的文件名,而是由換行符隔開的一個(gè)文本。

xargs 命令的神奇之處在于,它將把這個(gè)由空格或換行符分割的文本轉(zhuǎn)換為下一個(gè)命令的單獨(dú)輸入。

[gliu@fedora work]$ cat flowers.txt | xargs du -h
4.0Klily.txt
4.0Kone_lotus.txt
16Krose.txt
4.0Kthree_lotus.txt
16Ktwo_lotus.txt

這相當(dāng)于將這些文件名提供給 du 命令:

[gliu@fedora work]$ du -h lily.txt one_lotus.txt rose.txt three_lotus.txt two_lotus.txt 
4.0Klily.txt
4.0Kone_lotus.txt
16Krose.txt
4.0Kthree_lotus.txt
16Ktwo_lotus.txt

怎么樣,意識(shí)到 xargs 命令的強(qiáng)大了嗎?

xargs 和 find:為彼此而存在

xargs 命令經(jīng)常和 find 命令結(jié)合使用。

find 命令搜索文件和目錄并返回它們的名稱,有了 xargs 命令,你就可以將 find 命令的結(jié)果用于特定目的,如重命名、移動(dòng)、刪除等等。

比如,我們要搜索所有包含 red 一詞的txt文件,可以在 xargs 的幫助下結(jié)合 find 和

grep 命令:
[gliu@fedora work]$ find . -type f -name "*.txt" | xargs grep -l red
./three_lotus.txt
./two_lotus.txt
./rose.txt

find 與 exec 命令組合的工作原理類似。不過我們今天只集中討論 xargs 命令。

處理文件名中帶有空格的文件

如果文件名中有空格,會(huì)稍微麻煩點(diǎn)。比如我們將上面的文件three_lotus.txt重命名為 three lotus.txt,那么當(dāng)使用 xargs 處理時(shí),會(huì)被認(rèn)為是兩個(gè)獨(dú)立的文件,three 和 lotus.txt:

[gliu@fedora work]$ find . -type f -name "*.txt" | xargs grep -l red
./two_lotus.txt
grep: ./three: No such file or directory
grep: lotus.txt: No such file or directory
./rose.txt

在這種情況下,可以使用 find 命令的 -print0 選項(xiàng),它使用 ASCII null 字符來換行,而不是換行符;同時(shí),xargs 命令也需要帶 -0 選項(xiàng)。

[gliu@fedora work]$ find . -type f -print0 -name "*.txt" | xargs -0 grep -l red
./two_lotus.txt
./three lotus.txt
./rose.txt

查看正在執(zhí)行的命令

xargs 命令的 -t 選項(xiàng),會(huì)打印正在執(zhí)行的實(shí)際命令,所以可以用來查看正在執(zhí)行的命令。

[gliu@fedora work]$ find . -type f -name "*.txt" | xargs -t touch
touch ./three_lotus.txt ./two_lotus.txt ./lily.txt ./rose.txt

在運(yùn)行命令之前,強(qiáng)制 xargs 提示確認(rèn)

有些情況需要格外小心,比如刪除文件,最好看看要執(zhí)行什么命令,必要的話可以選擇拒絕執(zhí)行。

可以使用 -p 選項(xiàng),在執(zhí)行前進(jìn)行確認(rèn):?

[gliu@fedora work]$ find . -type f -name "*.txt" | xargs -p rm
rm ./three_lotus.txt ./two_lotus.txt ./lily.txt ./rose.txt ?...n

結(jié)合占位符的使用

默認(rèn)情況下,xargs命令將標(biāo)準(zhǔn)輸入作為參數(shù)添加到命令末尾。當(dāng)需要在最后一個(gè)參數(shù)之前使用時(shí),這會(huì)產(chǎn)生問題。

比如,使用 move 命令,首先需要一個(gè)源,然后需要一個(gè)目標(biāo)作為參數(shù);如果要將找到的文件移動(dòng)的目標(biāo)文件,那么將不起作用:?

[gliu@fedora work]$ find . -type f -name "*.txt" | xargs -p mv new_dir
mv new_dir ./three_lotus.txt ./two_lotus.txt ./lily.txt ./rose.txt ?...y
mv: target './rose.txt' is not a directory

這時(shí)候,可以用 -l 選項(xiàng)來使用占位符:?

[gliu@fedora work]$ find . -type f -name "*.txt" | xargs -p -I {} mv {} new_dir
mv ./three_lotus.txt new_dir ?...n
mv ./two_lotus.txt new_dir ?...n
mv ./lily.txt new_dir ?...n
mv ./rose.txt new_dir ?...n

上述命令中,xargs 從 find 命令中獲取所有的文件名,將其保存在 {} 中,然后轉(zhuǎn)到 mv 命令并提供 {} 中的內(nèi)容。

這里有一個(gè)主要的區(qū)別,它不是將所有文件名放在同一個(gè)命令中,而是逐個(gè)添加。這就是每個(gè)參數(shù)都會(huì)單獨(dú)調(diào)用 mv 命令的原因。

注:上述命令中使用 {} 作為占位符,你可以使用其他字符作為占位符。{} 是安全的,易于理解和區(qū)分。

使用xargs運(yùn)行多個(gè)命令

可以使用 xargs 的占位符來運(yùn)行多個(gè)命令:?

[gliu@fedora work]$ find . -type f -name "*.txt" | xargs -I {} sh -c 'ls -l {}; du -h {}' 
-rw-rw-r-- 1 gliu gliu 0 May 28 17:02 ./three_lotus.txt
0./three_lotus.txt
-rw-rw-r-- 1 gliu gliu 0 May 28 17:02 ./two_lotus.txt
0./two_lotus.txt
-rw-rw-r-- 1 gliu gliu 0 May 28 17:02 ./lily.txt
0./lily.txt
-rw-rw-r-- 1 gliu gliu 0 May 28 17:02 ./rose.txt
0./rose.txt

這里需要注意下,占位符不會(huì)擴(kuò)展到下一個(gè)管道重定向或者其他命令,這就是上述命令中為什么會(huì)使用 sh 命令的原因。

本文主要介紹了常用的 find 和 xargs 命令的使用,但 xargs 命令不是僅限于和find一起用。xargs 命令的一個(gè)很實(shí)際的例子是當(dāng)要停止所有正在運(yùn)行的 docker 容器時(shí):

docker ps -q | xargs docker stop

與其他 Linux 命令一樣,xargs 也有很多選項(xiàng)。關(guān)于詳細(xì)信息,大家可以查閱 xargs 命令的 man 手冊(cè)。

責(zé)任編輯:龐桂玉 來源: TIAP
相關(guān)推薦

2020-08-24 12:37:54

Linuxxargs命令

2023-02-28 22:26:33

2018-05-16 10:32:06

Linux命令find

2018-06-26 09:15:24

Linux命令history

2022-11-18 10:16:26

Linuxwc 命令

2023-09-14 15:05:33

grep正則表達(dá)式

2021-01-13 09:40:26

Linuxexport命令

2023-01-31 17:36:22

IPLinux網(wǎng)絡(luò)

2021-07-13 07:52:02

Linuxgrep命令

2023-11-22 07:54:33

Xargs命令Linux

2018-04-28 09:12:42

Linux

2021-07-20 10:00:28

Linuxgrep命令

2022-05-25 08:41:48

Linuxfd 命令文件

2019-09-16 19:00:48

Linux變量

2022-11-21 12:06:24

fgrep命令Linux

2022-03-30 09:32:32

BashtestLinux

2018-07-26 09:55:44

Linux命令升級(jí)軟件

2018-01-09 09:00:01

Linux命令文件壓縮

2022-11-23 08:14:42

bash 腳本test 命令

2023-01-09 10:29:41

mv命令Linux
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)