xargs 是 Unix 类零碎中的命令,该零碎从规范输出中读取我的项目,由空白(能够用双引号或单个引号或后斜线爱护)或新行界定,并执行命令(默认 /bin/echo)一次或屡次,规范输出上的空白行被疏忽。

xargs 命令与其余命令联合应用十分不便。默认状况下,它冀望来自 STDIN 的输出。xargs 次要用于加强初始命令的输入,并利用输入执行大量操作。

在这篇文章中,咱们将探讨 11 个 linux xargs 命令的理论示例

(1) 根本用法

输出 xargs,它将冀望咱们输出,输出完一行,以 enter 完结,再输出下一行,而后执行 ctrl+d 查看输入,如下所示

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

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

应用选项 -d 指定分隔符,并应用 \n 作为分隔符。当按下 ctrl+d 时,它将回显到屏幕上

[root@linuxtechi ~]# xargs -d\nHiWelcome hereNow press Ctrl+DHiWelcome hereNow press Ctrl+D[root@linuxtechi ~]#

(3) 限度每行输入 (-n)

能够在 xargs 命令中应用 -n 选项依据须要限度输入,例如每行只显示 2 项

linuxtechi@mail:~$ echo a1 b2 c3 d4 e45a1 b2 c3 d4 e5linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -n 2a1 b2c3 d4e5linuxtechi@mail:~$

(4) 在执行前启用用户提醒 (-p)

在 xargs 命令中应用选项 -p,执行前会提醒用户 y (是) 和 n (否)

linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2/echo a1 b2 ?...ya1 b2echo c3 d4 ?...yc3 d4echo e5 ?...nlinuxtechi@mail:~$ linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2/echo a1 b2 ?...ya1 b2echo c3 d4 ?...yc3 d4echo e5 ?...ye5linuxtechi@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_namelinuxtechi@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.txtlinuxtechi@mail:~$ find /tmp -name "*.txt" -print0 | xargs -0 rmlinuxtechi@mail:~$

(8) xargs 与 cut 命令

为了演示,让咱们首先创立一个蕴含以下内容的 cars.txt

linuxtechi@mail:~$ cat cars.txtHundai,SantroHonda,MobilioMaruti,ErtigaSkoda,Fabia

显示第一列中的数据,如下所示

linuxtechi@mail:~$ cut -d, -f1 cars.txt | sort | xargsHonda Hundai Maruti Skodalinuxtechi@mail:~$

(9) 计算每个文件的行数

linuxtechi@mail:~$ ls -1 *.txt | xargs wc -l4 cars.txt13 trucks.txt17 totallinuxtechi@mail:~$

(10) 将文件挪动到不同的地位

linuxtechi@mail:~$ pwd/home/linuxtechilinuxtechi@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.shlinuxtechi@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.shlinuxtechi@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

我的开源我的项目

  • course-tencent-cloud(酷瓜云课堂 - gitee仓库)
  • course-tencent-cloud(酷瓜云课堂 - github仓库)