天天看點

linux下比較難懂的 xargs 指令,看示例加深了解

linux下比較難懂的 xargs 指令,看示例加深了解

xargs 指令執行個體教學

xargs 是 Unix 類系統中的指令,該系統從标準輸入中讀取項目,由空白(可以用雙引号或單個引号或後斜線保護)或新行界定,并執行指令(預設 /bin/echo)一次或多次,标準輸入上的空白行被忽略。

xargs 指令與其他指令結合使用非常友善。預設情況下,它期望來自 STDIN 的輸入。xargs 主要用于增強初始指令的輸出,并利用輸出執行大量操作。

在這篇文章中,我們将讨論 11 個 linux xargs 指令的實際示例

(1) 基本用法

輸入 xargs,它将期望我們輸入,輸入完一行,以 enter 結束,再輸入下一行,然後執行 ctrl+d 檢視輸出,如下所示

linuxtechi@mail:~$ xargs
hello
john
this is me ( ctrl+d)
hello john this is me
linuxtechi@mail:~$home/Downloads#           

(2) 使用分隔符 (-d)

使用選項 -d 指定分隔符,并使用 \n 作為分隔符。當按下 ctrl+d 時,它将回顯到螢幕上

[root@linuxtechi ~]# xargs -d\n
Hi
Welcome here
Now press Ctrl+D
Hi
Welcome here
Now press Ctrl+D

[root@linuxtechi ~]#           

(3) 限制每行輸出 (-n)

可以在 xargs 指令中使用 -n 選項根據需要限制輸出,例如每行隻顯示 2 項

linuxtechi@mail:~$ echo a1 b2 c3 d4 e45
a1 b2 c3 d4 e5
linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -n 2
a1 b2
c3 d4
e5
linuxtechi@mail:~$           

(4) 在執行前啟用使用者提示 (-p)

在 xargs 指令中使用選項 -p,執行前會提示使用者 y (是) 和 n (否)

linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2
/echo a1 b2 ?...y
a1 b2
echo c3 d4 ?...y
c3 d4
echo e5 ?...n
linuxtechi@mail:~$ 
linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2
/echo a1 b2 ?...y
a1 b2
echo c3 d4 ?...y
c3 d4
echo e5 ?...y
e5
linuxtechi@mail:~$           

(5) 使用 find 和 xargs 删除檔案

假設我們要從 /tmp 檔案夾删除 *.txt 檔案,運作以下指令

linuxtechi@mail:~$ find /tmp  -type f -name '*.txt' | xargs rm           

注意: 始終建議使用上述 find 和 xargs 指令的組合删除 1000+ 檔案,因為它節省時間和系統資源。

(6) 使用 xargs 和 grep 指令進行搜尋

可以使用 grep 指令和 xargs 從 find 指令的結果中篩選特定的搜尋。

linuxtechi@mail:~$ find . -name "stamp" | xargs grep "country"
country_name
linuxtechi@mail:~$           

(7) 處理檔案名中的空格

xargs 還可以通過使用 print0 和 xargs -0 參數來處理 find 指令查找檔案中的空格。

linuxtechi@mail:~$ find /tmp -name "*.txt" -print0 | xargs -0 ls
/tmp/abcd asd.txt /tmp/asdasd asdasd.txt /tmp/cdef.txt

linuxtechi@mail:~$ find /tmp -name "*.txt" -print0 | xargs -0 rm
linuxtechi@mail:~$           

(8) xargs 與 cut 指令

為了示範,讓我們首先建立一個包含以下内容的 cars.txt

linuxtechi@mail:~$ cat cars.txt
Hundai,Santro
Honda,Mobilio
Maruti,Ertiga
Skoda,Fabia           

顯示第一列中的資料,如下所示

linuxtechi@mail:~$ cut -d, -f1 cars.txt | sort | xargs
Honda Hundai Maruti Skoda
linuxtechi@mail:~$
           

(9) 計算每個檔案的行數

linuxtechi@mail:~$ ls -1 *.txt | xargs wc -l
4 cars.txt
13 trucks.txt
17 total
linuxtechi@mail:~$           

(10) 将檔案移動到不同的位置

linuxtechi@mail:~$ pwd
/home/linuxtechi
linuxtechi@mail:~$ ls -l *.sh
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcde.sh
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcd.sh
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 fg.sh

linuxtechi@mail:~$ sudo find . -name "*.sh" -print0 | xargs -0 -I {} mv {} backup/
linuxtechi@mail:~$ ls -ltr backup/
total 0
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcd.sh
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcde.sh
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 fg.sh
linuxtechi@mail:~$           

(11) 替換指令中的字元串 (-i)

如果我們運作下面的指令,它将在目前工作目錄中建立三個檔案 a、b 和 c

linuxtechi@mail:~$ printf "a\nb\nc\n" | xargs touch           

如果你想建立 a.txt, b.txt 和 c.txt,那麼在 xargs 指令中使用 -i 參數,它将用 a 替換為 a.txt,以此類推,示例如下:

linuxtechi@mail:~$ printf "a\nb\nc\n" | xargs -i touch {}.txt           

我的開源項目

linux下比較難懂的 xargs 指令,看示例加深了解

開源知識付費解決方案

  • course-tencent-cloud(酷瓜雲課堂 - gitee 倉庫)
  • course-tencent-cloud(酷瓜雲課堂 - github 倉庫)

繼續閱讀