linux xargs命令

The xargs command allows us to pass the output of one command as the input for another command without having to write the output in a file.

xargs命令允许我们传递一个命令的输出作为另一命令的输入,而不必将输出写入文件。

Some tasks that may require the use of multiple commands that cross-reference outputs from other commands will be greatly helped with the use of xargs. Let’s have a look at understanding the command and some of the uses of the command today.

使用xargs可以极大地帮助某些可能需要使用多个命令来交叉引用其他命令的输出的任务。 让我们看一下今天对命令的了解以及该命令的一些用法。

什么是xargs? (What is xargs?)

Xargs allows you to pass outputs as input to another command. While a command like grep allows the user to use inputs as parameters, xargs shows its importance by allowing the input to be in the form of arguments.

Xargs允许您将输出作为输入传递给另一个命令。 尽管类似grep的命令允许用户使用输入作为参数,但是xargs通过允许输入采用参数形式来显示其重要性。

This command is almost always used in combination with other commands with the help of piping. Let’s understand the basic usage of the command.

借助管道,几乎总是将此命令与其他命令结合使用。 让我们了解该命令的基本用法。

Linux中xargs命令的基础 (Basics of xargs Command in Linux)

The best way to understand any command is through understanding it’s syntax. Here is how the syntax for the xargs command in Linux looks like.

理解任何命令的最佳方法是通过了解其语法。 这是Linux中xargs命令的语法。


xargs [option] [command]

Here, the [command] can be any command(s) which we wish to execute on our standard input. If we don’t specify the commands, the xargs utility uses the default command.

在此,[command]可以是我们希望在标准输入上执行的任何命令。 如果不指定命令,则xargs实用程序将使用默认命令。

The default command for the xargs utility is ‘echo’. 

xargs实用程序的默认命令是“ echo”。

The xargs utility allows several options to dictate how commands are built and executed. Here is a list of some of the commonly used options offered by the xargs utility in Linux.

xargs实用程序允许使用几个选项来指示如何构建和执行命令。 这是Linux中xargs实用程序提供的一些常用选项的列表。

Option Effect
-0 This option is used to specify to the xargs utility that the items in the standard input string are terminated using a NULL character instead of a white space.
-a filename Read from file
-r Don’t run if the input is empty
-d delim Specify a delimiter
-x Exit if the input size specified by -s is exceeded.
-I replace-str This is used to declare a string as replace-str. Then all occurrences of replace-str are swapped by the parameter or argument from the standard input.
–help This option is used to display the help message for the xargs utility. This includes all the possible options which can be used with the xargs utility.
选项 影响
-0 此选项用于向xargs实用程序指定标准输入字符串中的项目以NULL字符而不是空格终止。
-a 文件名 从文件读取
-r 如果输入为空,则不运行
-d 德林 指定定界符
-X 如果超过了-s指定的输入大小,则退出。
-我替换-str 这用于将字符串声明为replace-str。 然后,所有出现的replace-str都由标准输入中的参数或自变量交换。
-救命 此选项用于显示xargs实用程序的帮助消息。 这包括可以与xargs实用程序一起使用的所有可能的选项。

While this list covered the basic options used in the xargs utility. Don’t forget to explore the man page for xargs.

该列表涵盖了xargs实用程序中使用的基本选项。 不要忘记浏览xargs的手册页 。

使用xargs命令 (Using the xargs command)

Now we have developed an understanding of the xargs command in Linux and its parameters. It’s time to use this knowledge for the application of the xargs utility. For this tutorial, we will go over some examples to learn how to use the xargs utility.

现在,我们已经了解了Linux中的xargs命令及其参数。 现在是时候将这些知识用于xargs实用程序的应用程序了。 在本教程中,我们将通过一些示例来学习如何使用xargs实用程序。

xargs默认行为 (The xargs Default Behavior)

As we mentioned earlier, the xargs command will run the echo command by default for the input arguments if no command is specified after it.

如前所述,如果在输入参数之后未指定任何命令,则xargs命令将默认对输入参数运行echo命令。

Let’s see that in action to verify our claim.

让我们来看看实际行动以验证我们的主张。


ls -ls | xargs
Xargs Default
Xargs默认

As you can see in the above screenshot, the output for the ls command gets printed out without the line breaks and formatting when passed through the echo and the xargs command.

从上面的屏幕快照中可以看到,通过echo和xargs命令传递时, ls命令的输出被打印出来,而没有换行符和格式。

使用带有名称列表的文件创建多个目录 (Create Multiple Directories Using a File with List of Names)

If you have the list of names in a single line, you can copy and paste the directory names separated by spaces and mkdir command will create the directories.

如果您将名称列表放在一行中,则可以复制并粘贴用空格分隔的目录名称,然后mkdir命令将创建目录。

But what if we have a file with the list of all the names of directories we want to create? Let’s see how we can create multiple directories from a list of directory names.

但是,如果我们有一个包含要创建的所有目录名称列表的文件,该怎么办? 让我们看看如何从目录名称列表中创建多个目录。


cat <filename> | xargs mkdir
Xargs Pipe Output
Xargs管道输出

As you can see in the above screenshot, we tried to directly pipe the output of the cat command to mkdir and failed. But with the addition of xargs, we were able to execute the command and successfully create the directories.

如您在上面的屏幕截图中所见,我们尝试将cat命令的输出直接通过管道传递到mkdir并失败。 但是通过添加xargs,我们能够执行命令并成功创建目录。

查找包含特定字符串的文件 (Find Files Containing a Specific String)

Grep can search for strings within files or output messages. But it cannot search for files. The find command can search for files, but not the contents within. The xargs command can help connect both of these together.

Grep可以在文件或输出消息中搜索字符串。 但是它无法搜索文件。 find命令可以搜索文件,但不能搜索其中的内容。 xargs命令可以帮助将这两个都连接在一起。


find <search directory> -name "<name>" | xargs grep -E 'fatal|Warning'
Find Xargs
查找Xargs

We searched for the file “syslog” in the /var/log directory and looked for lines containing the words “fatal” or “Warning”. Since we knew the file we want in this case, we could have directly passed it on to grep. But if we did not know the file path, the find command can be used to find the file and pass the paths to grep.

我们在/ var / log目录中搜索文件“ syslog”,并查找包含单词“ fatal”或“ Warning”的行。 由于我们知道在这种情况下所需的文件,因此我们可以将其直接传递给grep。 但是,如果我们不知道文件路径,则可以使用find命令查找文件并将路径传递给grep。

删除具有特定扩展名的文件 (Delete Files Having Specific Extensions)

Like creating and identifying files and directories, the xargs utility is also helpful in getting rid of files from your system. For this example, we will delete all the text files present in our current directory.

像创建和标识文件和目录一样,xargs实用程序也有助于从系统中删除文件。 对于此示例,我们将删除当前目录中存在的所有文本文件。


find <path> -name <filename> | xargs rm -rf
Delete Files Xargs
删除文件Xargs

Here, the find command first searches for files that have the .txt extension. This output is then sent to the rm command.

在这里, find命令首先搜索扩展名为.txt的文件。 然后将此输出发送到rm命令。

This command takes the files from the find command and deletes them. The output of the successful execution of this command should give and output similar to the one given below.

此命令从find命令获取文件并删除它们。 成功执行此命令的输出应给出并输出与以下给出的类似。

结论 (Conclusion)

The xargs command in Linux is a powerful tool to build and execute commands using a standard input through your command line. It makes your command line usage efficient by allowing you to use the output of one command in place of an argument in another command.

Linux中的xargs命令是使用命令行中的标准输入来构建和执行命令的强大工具。 通过允许您使用一个命令的输出代替另一个命令中的参数,可以提高命令行的使用效率。

This command-line utility is widely used by both beginner and experienced Linux users regularly.

初学者和有经验的Linux用户都会定期使用此命令行实用程序。

We hope this tutorial was able to help you understand the xargs command in Linux. We discussed only the basic usage of the xargs utility in this tutorial, so make sure to explore the command on your own.

我们希望本教程能够帮助您了解Linux中的xargs命令。 在本教程中,我们仅讨论了xargs实用程序的基本用法,因此请确保自己探索该命令。

If you have any feedback, queries or suggestions, feel free to reach out to us in the comments below.

如果您有任何反馈,疑问或建议,请随时通过以下评论与我们联系。

翻译自: https://www.journaldev.com/40105/xargs-command-in-linux

linux xargs命令

linux xargs命令_如何在Linux中使用xargs命令?相关推荐

  1. python执行的命令_如何在Python中执行外部命令

    Python子进程模块允许生成新进程,从Python脚本执行外部命令. 您可以使用这些教程来安装最新版本的Python. 此外,还有许多可用于Python IDE. 就像在Ubuntu系统上安装PyC ...

  2. linux查看图像大小_如何在Linux上调整一批图像的大小?

    linux查看图像大小 Resizing images on Linux with gThumb is easy. However, I have a batch of images inside a ...

  3. linux重启网卡命令_如何在 Linux 中更改 MAC 地址 | Linux 中国

    在向你展示如何在 Linux 中更改 Mac 地址之前,让我们首先讨论为什么要更改它.-- Dimitrios Savvopoulos 在向你展示如何在 Linux 中更改 MAC 地址之前,让我们首 ...

  4. linux使用find命令_如何在Linux中使用FIND

    linux使用find命令 在最近的Opensource.com文章中 ,刘易斯·考尔斯介绍了find命令. 在日常工具箱中, find是功能更强大,更灵活的命令行程序之一,因此值得花一些时间在上面. ...

  5. linux终端删除文件命令_如何在Linux终端中删除文件和目录

    linux终端删除文件命令 Fatmawati Achmad Zaenuri/Shutterstock.comFatmawati Achmad Zaenuri / Shutterstock.com T ...

  6. linux查找文件夹命令_如何在Linux中使用命令行查找文件和文件夹

    linux查找文件夹命令 Most people use a graphical file manager to find files in Linux, such as Nautilus in Gn ...

  7. linux ntp时间立即同步命令_如何在 Linux 下确认 NTP 是否同步?

    我假设我你经使用上述链接设置了 NTP 服务器和 NTP 客户端.现在,如何验证 NTP 设置是否正常工作? -- Magesh Maruthamuthu(作者) NTP 意即 网络时间协议(Netw ...

  8. linux grep 排除_如何在Linux中排除Grep?

    linux grep 排除 grep is very useful tool used by a lot of tech guys. grep provides different functions ...

  9. linux bash 变量_如何在Linux上的Bash中设置环境变量

    linux bash 变量 fatmawati achmad zaenuri/Shutterstock Fatmawati achmad zaenuri / Shutterstock There's ...

最新文章

  1. grep如何忽略.svn目录,以及如何忽略多个目录
  2. python程序实例电话本-Python基于递归实现电话号码映射功能示例
  3. 微信小程序签名(横屏+竖屏)
  4. 基于注释的Spring Security实战
  5. 腾讯 java_2019腾讯的面试题(腾讯qq音乐部门)
  6. AcWing 274. 移动服务
  7. java某个时间推迟60天_java计算两个时间相差(天、小时、分钟、秒)
  8. 使用windows的事件查看器(eventvwr),查看、电脑执行过的你不知道的操作・开机・关机时间
  9. 【服务器】【个人网盘】宝塔安装OneIndex
  10. C语言控制台窗口图形界面编程(八):鼠标事件
  11. 华为程序员:加了六天班,加班费一万四,网友:我能加到它破产
  12. 【cluvfy】集群验证工具cluvfy使用方法——stage
  13. OOB模式下Exit事件的处理
  14. gitbook 配置
  15. 用粉红噪声煲机_白噪音|煲机白噪音粉红噪音mp3下载 - 121下载站
  16. 2021-11-13偏最小二乘法应用实例python程序代码
  17. 腾科张老师教你如何在cisco路由器上部署和使用FTP/TFTP
  18. wangEditor富文本编辑器自定义图片上传
  19. Cauchy distribution
  20. Cause: java.sql.SQLExceptioValue ‘0000-00-00 00:00:00‘ can not be represented as java.sql.Timestamp

热门文章

  1. Web页面打印及GridView导出到Excel
  2. Python 基础课程第十一天
  3. Eclipse下创建Spring MVC web程序--非maven版
  4. 〖Demo〗-- 多级评论
  5. 带你玩转JavaWeb开发之四 -如何用JS做登录注册页面校验
  6. 避免使用PHP保留字作为常量、类名和方法名,以及命名空间的命名
  7. Swift 提示 error running playground...
  8. golang语言os.Stat()用法及功能
  9. PS中的Workflow的创建
  10. windows下如何使用QT编写dll程序 .