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

「爱情、让人受尽委屈。」 2023-02-28 06:27 141阅读 0赞

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命令的语法。

  1. 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.

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

  1. 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.

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

  1. 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命令可以帮助将这两个都连接在一起。

  1. 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实用程序也有助于从系统中删除文件。 对于此示例,我们将删除当前目录中存在的所有文本文件。

  1. 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 条评论,141人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Linux xargs命令介绍

      Linux 中的 `xargs` 命令是一个非常有用的命令行工具,可以将一些参数集合传递给其他命令作为参数,并利用指定的命令进行处理。它可以帮助我们批量处理文件,执行一些需

    相关 Linux xargs 命令

    xargs(英文全拼: eXtended ARGuments)是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。 xargs 可以将管道或标准输入(stdin)数据转

    相关 记录一linux命令 xargs

    分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来![https://blog.csdn.net/jiangjuns

    相关 Linux xargs 命令

    xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。 xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。...