linux 常用命令ppt(xargs命令的使用场景)
linux 常用命令ppt(xargs命令的使用场景)
2024-07-04 11:18:10  作者:抱琴问知音  网址:https://m.xinb2b.cn/know/cdv335168.html

man xargs是这样描述xargs的用途:

xargs is used to build and execute command lines from standard input.

中文意思大致是,xargs是基于标准输入构建和执行命令行。

Linux系统中一个命令的语法是: 命令名称 [命令选项] [命令参数]。其中,命令选项是以-或--开头的,命令选项的数量有时不止一个,可以有多个;命令参数是命令的操作对象,通常为文件名或目录名等,命令参数的数量有时不止一个,可以有多个。

xargs的语法是: xargs [xargs的命令选项] 其他命令的名称 [其他命令的命令选项] [其他命令的命令参数]。

xargs为它后面出现的其他命令构建一个或多个命令参数,或者说,xargs为它后面出现的其他命令构建标准输入(stdin)。

xargs的英文全称是executable arguments,指可执行的命令参数。或许这样理解更好一些,xargs is used to build executable arguments for a command from standard input。

例子1: xargs的标准输出是什么?xargs的标准输出默认是一行命令参数

xargs命令,是将标准输入拆分成多个命令参数。既然是拆分,就需要有分隔符去分隔标准输入,这个默认的分隔符就是空格或换行符。

默认时,xargs命令是将标准输入转换为一行命令参数(注意: 是一行而不是多行),这行命令参数是为其他命令构建好的一个或多个命令参数,每个命令参数之间以一个空格间隔开。

一行命令参数 = 命令参数1 命令参数2 命令参数3 .. 命令参数N。

root@hgdm:~/examples# find ~/examples/ -name '*data*'/root/examples/cut_data.txt/root/examples/data333.txt/root/examples/data555.txt/root/examples/xargs_data.txt/root/examples/data1.txt/root/examples/sed_data.txt/root/examples/data2.txt/root/examples/data22.txt/root/examples/user_data.txt/root/examples/data111.txt/root/examples/data55.txt/root/examples/data444.txt/root/examples/data3.txt/root/examples/data11.txt/root/examples/sed_data2.txt/root/examples/sed_data_new.txt/root/examples/data4.txt/root/examples/data5.txt/root/examples/data44.txt/root/examples/data33.txt/root/examples/data222.txt# xargs命令把标准输入的数据(find命令的输出数据)转换成了一行数据,这行数据每个命令参数之间以一个空格分隔开root@hgdm:~/examples# find ~/examples/ -name '*data*' | xargs/root/examples/cut_data.txt /root/examples/data333.txt /root/examples/data555.txt /root/examples/xargs_data.txt /root/examples/data1.txt /root/examples/sed_data.txt /root/examples/data2.txt /root/examples/data22.txt /root/examples/user_data.txt /root/examples/data111.txt /root/examples/data55.txt /root/examples/data444.txt /root/examples/data3.txt /root/examples/data11.txt /root/examples/sed_data2.txt /root/examples/sed_data_new.txt /root/examples/data4.txt /root/examples/data5.txt /root/examples/data44.txt /root/examples/data33.txt /root/examples/data222.txt

例子2: xargs命令为grep命令构建标准输入(命令参数)

root@hgdm:~/examples# cat xargs_data.txt If there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)Intelligence is not the ability to store information, but to know where to find it. (Albert Einstein)When you make a mistake, there are only three things you should ever do about it: admit it, learn from it, and don't repeat it. (Bear Bryant)# 此时,grep命令的标准输入是'xargs_data.txt'这个字符串root@hgdm:~/examples# echo 'xargs_data.txt' | grep 'data'xargs_data.txt# 此时,grep命令的标准输入是'xargs_data.txt'这个字符串,而first things first不在这个字符串里,所以找不到内容root@hgdm:~/examples# echo 'xargs_data.txt' | grep 'first things first'root@hgdm:~/examples# # 此时,grep命令为grep命令构建了标准输入,把字符串'xargs_data.txt'转换为文件xargs_data.txt# 所以,这里grep命令的标准输入是xargs_data.txt这个文件,而不是'xargs_data.txt'这个字符串root@hgdm:~/examples# echo 'xargs_data.txt' | xargs grep 'first things first'If there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)

linux 常用命令ppt(xargs命令的使用场景)(1)

xargs为grep构建标准输入

例子3: xargs命令为cat命令构建标准输入(命令参数)

root@hgdm:~/examples# cat xargs_data.txt If there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)Intelligence is not the ability to store information, but to know where to find it. (Albert Einstein)When you make a mistake, there are only three things you should ever do about it: admit it, learn from it, and don't repeat it. (Bear Bryant)# 此时,cat命令的标准输入是'xargs_data.txt'这个字符串root@hgdm:~/examples# echo 'xargs_data.txt' | catxargs_data.txt# 此时,xargs命令为cat命令构建了标准输入,把字符串'xargs_data.txt'转换为文件xargs_data.txt# 所以,这里cat命令的标准输入是xargs_data.txt这个文件root@hgdm:~/examples# echo 'xargs_data.txt' | xargs catIf there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)Intelligence is not the ability to store information, but to know where to find it. (Albert Einstein)When you make a mistake, there are only three things you should ever do about it: admit it, learn from it, and don't repeat it. (Bear Bryant)

linux 常用命令ppt(xargs命令的使用场景)(2)

xargs为cat构建标准输入

例子4: xargs命令为wc命令构建标准输入(命令参数)

wc -l命令是用于统计一个标准输入里有几行数据。

root@hgdm:~/examples# cat xargs_data.txt If there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)Intelligence is not the ability to store information, but to know where to find it. (Albert Einstein)When you make a mistake, there are only three things you should ever do about it: admit it, learn from it, and don't repeat it. (Bear Bryant)# wc的标准输入是'xargs_data.txt'这个字符串,只有一行数据,因此输出1root@hgdm:~/examples# echo 'xargs_data.txt' | wc -l1# xargs命令为wc命令构建了标准输入,把字符串'xargs_data.txt'转换为文件xargs_data.txt# 所以,这里wc命令的标准输入是xargs_data.txt这个文件,而xargs_data.txt里有4行数据,因此输出4root@hgdm:~/examples# echo 'xargs_data.txt' | xargs wc -l 4 xargs_data.txtroot@hgdm:~/examples#

linux 常用命令ppt(xargs命令的使用场景)(3)

xargs为wc构建标准输入

例子5:xargs -d -n-d命令选项,用于指定xargs标准输入(stdin)的每个参数之间的分隔符,默认的分隔符为空格或换行符(\n)。-n命令选项,用于指定xargs标准输出(stdout)的每行最多含几个参数。不使用-n,默认情况下,xargs的标准输出是一行命令参数,这行命令参数里每个参数之间以一个空格分隔开。

root@hgdm:~/examples# echo '孤:舟:蓑:笠:翁:独:钓:寒:江:雪' | xargs -d :孤 舟 蓑 笠 翁 独 钓 寒 江 雪root@hgdm:~/examples# echo '孤:舟:蓑:笠:翁:独:钓:寒:江:雪' | xargs -d : -n 2孤 舟蓑 笠翁 独钓 寒江 雪root@hgdm:~/examples#

linux 常用命令ppt(xargs命令的使用场景)(4)

xargs -d -n

例子6:xargs -0

-0这个命令选项用于指定标准输入(stdin)的参数分隔符为null字符,而不是使用默认空格或换行符作为参数分隔符。当标准输入的参数里包含空格、换行符、引号或反斜杠时,-0是非常有用的。

比较以下两个命令的区别

# 这个find命令输出的每个目录或文件是以null字符作为分隔符的,# 假若find输出的目录名或文件名含有空格或换行符,# 那么,xargs -0为rm构造标准输入时也能准确区分这些含空格或换行符的目录或文件,# 执行rm时不会出现删错的情况。find ~/examples/ -name '*data*' -print0 | xargs -0 rm -rf

# 这个find命令输出的每个目录或文件是以换行符作为分隔符,# 假若find输出的目录名或文件名含有空格或换行符,# 那么xargs为rm构造标准输入时,就不能准确区分这些含空格或换行符的目录或文件,把含空格或换行符的目录名或文件名也拆分成命令参数,# 执行rm时可能会出现删错的情况。find ~/examples/ -name '*data*' -print | xargs rm -rf

  • 漫游潮州八景(漫游潮州八景)
  • 2024-07-05漫游潮州八景潮州岭海名邦,岭东首邑潮州,位于广东东部,东与福建省接壤,是国家历史文化名城、潮州文化的重要发源地,素有“岭海名邦”、“岭东首邑”等美誉潮州有众多属于自己的独特文化:潮绣、潮州戏、潮州木雕、潮州菜、潮。
  • 吃小白菜有什么好处(盘点吃小白菜的好处)
  • 2024-07-05盘点吃小白菜的好处缓解精神紧张,小白菜中的维生素B、维生素B泛酸等,具有缓解精神紧张的功能,多吃有助于保持平静的心态吃白菜能补钙,大白菜含有丰盛的钙,每克中含钙毫克一杯熟的大白菜汁能够供给几乎与一杯牛奶一样多的钙有的人。
  • 拉花咖啡入门基础知识(烘焙小课堂一篇搞定)
  • 2024-07-05烘焙小课堂一篇搞定据说,喜欢喝速溶咖啡的人时间观念很强,直爽;那么喜欢研磨咖啡的人应该很恬淡,他们注重生活品位,具备一些随遇而安的特质而在意咖啡拉花的人,更喜欢享受生活的情调咖啡拉花就像一门艺术,能营造美的观感,也能给。
  • 江西糕点有哪些(江西的这几款传统糕点)
  • 2024-07-05江西的这几款传统糕点每当临近年关总会被各种各样的年味所吸引除了新衣服、压岁钱还有着各种糕点让人怎么吃都吃不够今天大表哥就给大家推荐那些吃过后让人忘不掉的糕点都是江西的传统特色也可以尝试着自己动手试试哦~九江酥糖九江酥糖的。
  • 出走而又回归的娜拉 伤逝出走后的子君
  • 2024-07-05出走而又回归的娜拉 伤逝出走后的子君读完了鲁迅文集里的小说《伤逝》,合上书,我的眼里居然溢出了泪水,我的泪水是为小说里勇敢追求爱情,最终却因爱情而伤而逝的可怜的子君流下的《伤逝》是鲁迅先生1925年创作的短篇小说,也是他唯一描写男女爱情。
  • 智能家居设计的流程(智能家居设计是未来趋势)
  • 2024-07-05智能家居设计是未来趋势智能家居必然是未来装修的大趋势,它们具体而详细的成果展示,我们在影视作品中已经可以看到,而且是比现实中更为先进的理念早晨起来,窗帘自动打开,一缕阳光照进房间,房间里的一切井然有序的工作着,完全不需要个。
  • 巧克力和牛奶可以同时食用吗
  • 2024-07-05巧克力和牛奶可以同时食用吗牛奶和巧克力能一起吃,两者都是常见的食品,不存在任何的冲突牛奶营养丰富,含有人体所必需的八种氨基酸,包括赖氨酸、色氨酸、蛋氨酸、亮氨酸、异亮氨酸、苯丙氨酸、缬氨酸、苏氨酸这八种,而且还有丰富的钙质,可。
  • 有没有一尘不变的感情(有没有一种情感)
  • 2024-07-05有没有一种情感有没有一种情感可以纤尘不染?有没有一种情感,与暧昧与关;有没有一种情感,与情爱无关;有没有一种情感,与亲情无关有没有一种情感,不需要借你的胸膛,也不需要借你的双肩,只要你的温暖和真诚陪伴,高兴时可以一。
  • 节日营销技巧和策划(十一月节日营销活动指南)
  • 2024-07-05十一月节日营销活动指南在前面我们已经推出了《万圣节营销活动方案》与《双十一营销活动方案》,除此之外,十一月还有其它节日节气值得各位商家借势营销跟随雨科网营销活动看看这份《十一月节日营销活动指南》都有哪些内容吧11.7立冬/。
  • 给我一个什么我能撬动整个地球(我可以撬动地球)
  • 2024-07-05我可以撬动地球古希腊物理学家阿基米德曾经说过一句家喻户晓的名言:给我一个支点,我可以撬动地球即使找到那个支点,从哪找到那个可以撬动地球的杠杆,撬动地球只是阿基米德的妄想罢了01但是前一段时间发生的一件事情,似乎证实。