命令参数过滤器xargs

今天,我们来学习一下Linux中非常重要的命令参数过滤器:xargs !


xargs : 给其后续命令传递参数的过滤器;处理管道或stdin的输入,将其转换为命令参数;其默认命令为echo,默认分隔符为空格。


用途

1,用于组合多个命令,构成单行命令;
比如,ls命令不支持管道,但可通过xargs命令传递参数;

2,避免命令输入的参数过长;
可使用-n选项给参数分组,从而避免参数过长;

选项

  • -o 将管道中的特殊字符还原成一般字符;
  • -eSTRING 此选项意译为EOF,即End Of File,选项后紧跟一个字符串STRING,当xargs分析到STRING时,就结束分析;
  • -p 操作时进行交互式询问;
  • -n# 命令执行时,一次处理#个参数;
  • -dCHAR 紧跟CHAR,指定分隔符为CHAR;
  • I STRING 用xargs转换的参数替换下一命令中出现的字符串STRING;

用法详解

我们以test.txt作为测试文件来演示。

[root@localhost jeffrey]# cat test.txt
a b c d e f g
h i j k l m n
o p q r s t u
v w x y z

(1)单独使用,将管道或标准输入转换为一行参数,以空格为分隔符;

[root@localhost jeffrey]# cat test.txt | xargs
a b c d e f g h i j k l m n o p q r s t u v w x y z

(2)使用选项-e设置j为处理结束符;

[root@localhost jeffrey]# cat test.txt | xargs -ej
a b c d e f g h i

(3)使用选项-p,操作时进行询问;

[root@localhost jeffrey]# cat test.txt | xargs -p
echo a b c d e f g h i j k l m n o p q r s t u v w x y z ?...y
a b c d e f g h i j k l m n o p q r s t u v w x y z

(4)使用选项-n,命令执行时一次处理#个参数;一次处理4个参数,所以4个参数为一行;

[root@localhost jeffrey]# cat test.txt | xargs -n4
a b c d
e f g h
i j k l
m n o p
q r s t
u v w x
y z

(5)使用-d选项,设置分隔符为

[root@localhost jeffrey]# echo "cherry:Jeffrey:Darry:Karry" | xargs -d:
cherry Jeffrey Darry Karry

(6)使用-I选项,用参数替换下一命令中出现的STRING;查看所有以.txt结尾的文件,并删除之。

[root@localhost jeffrey]# ls *.txt
Jeffrey.txt  lovers.txt  save.txt  test.txt  Tom.txt
[root@localhost jeffrey]# ls *.txt | xargs -I '{}' rm {}

(7)使用-0选项,处理文件名中的特殊字符;
有时,我们的文件名中会出现空格,但是xargs以空格或换行符作为分隔符,所以会出现问题:hello hello.txt为一个文件,但是xargs却将其转换成两个参数;这是因为xargs将文件命令中的空格当成了分隔符;

[root@localhost jeffrey]# find -name 'hello hello.txt' -print | xargs -n1
./hello
hello.txt

因此,我们使用-0选项,将空格视为一般字符,而不是分隔符;

[root@localhost jeffrey]# find -name 'hello hello.txt' -print0 | xargs -n1 -0
./hello hello.txt
find命令是用于在指定目录下搜索文件和目录的命令xargs命令是一个过滤器,用于将标准输入或管道数据转换为命令参数,并将其传递给其他命令。在这里,find命令xargs命令可以结合使用来对系统中的文件进行操作。 例如,可以使用find命令查找系统中的每一个普通文件,并使用xargs命令来测试它们分别属于哪类文件。具体的命令如下: find . -type f -print | xargs file 这个命令会在当前目录及其子目录中查找所有的普通文件,并将它们的路径传递给xargs命令xargs命令会将每个文件路径作为参数传递给file命令,然后file命令会对每个文件进行类型判断并输出结果。 请注意,上述命令中的"."表示当前目录,"-type f"表示只搜索普通文件。你可以根据需要修改命令中的参数来满足你的具体需求。 #### 引用[.reference_title] - *1* *2* [浅谈 Find 以及xargs一些用法](https://blog.youkuaiyun.com/lpb2019/article/details/102476994)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Linux命令(三)find,xargs](https://blog.youkuaiyun.com/lingfy1234/article/details/107453631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值