linux xargs命令_如何在Linux中使用xargs命令?

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实用程序提供的一些常用选项的列表。

OptionEffect
-0This 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 filenameRead from file
-rDon’t run if the input is empty
-d delimSpecify a delimiter
-xExit if the input size specified by -s is exceeded.
-I replace-strThis 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.
–helpThis 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 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 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
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
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命令

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值