linux xargs命令_Linux xargs命令示例

linux xargs命令

The xargs command in Linux/UNIX reads items from the standard input, delimited by blanks or newlines, and executes the command one or more times with any initial arguments.

Linux / UNIX中的xargs命令从标准输入中读取由空格或换行符分隔的项目,并使用任何初始参数一次或多次执行该命令。

If no command is provided as an argument to xargs, then the default command that the tool executes is an echo.

如果没有提供命令作为xargs的参数,则该工具执行的默认命令是回显。

Blank lines on the standard input are ignored. The items those xargs read from standard input can be protected with double or single quotes or a backslash.

标准输入上的空白行将被忽略。 这些xargs从标准输入读取的项目可以用双引号或单引号或反斜杠进行保护。

The xargs command is extremely powerful, especially when combined with commands like grep or echo. In this tutorial, we will cover a few basic usages of xargs command to get you started with it.

xargs命令非常强大,尤其是与grep或echo这样的命令结合使用时。 在本教程中,我们将介绍xargs命令的一些基本用法,以帮助您入门。

Linux xargs命令语法 (Linux xargs Command Syntax)

The syntax of xargs command is:

xargs命令的语法为:

xargs [options] [command [initial-arguments]]

You can view all the options and arguments by invoking the command man xargs from the terminal. The following section list a few examples of args command.

您可以通过从终端调用命令man xargs来查看所有选项和参数。 以下部分列出了args命令的一些示例。

Linux xargs简单示例 (Linux xargs Simple Example)

The xargs command without any command echoes the arguments in the standard input. In the following example output from ls command is piped into the xargs command and the xargs command without any command echo the file list in the standard input.

不带任何命令的xargs命令将回显标准输入中的参数。 在下面的示例中,ls命令的输出通过管道传递到xargs命令中,而xargs命令在没有任何命令的情况下回显标准输入中的文件列表。

# ls | xargs
Linux xargs command examples

Xargs Example 1.a

Xargs示例1.a

The following example shows how the output of echo command is piped to xargs and the xargs command uses the piped argument to create folders using mkdir.

以下示例显示了echo命令的输出如何通过管道传递给xargs,xargs命令如何使用管道参数使用mkdir创建文件夹。

# echo 'one two three' | xargs mkdir
Xargs Example 1.b

Xargs Example 1.b

Xargs示例1.b

限制参数个数 (Limit Number of Arguments)

The number of arguments passed to the xargs is defined by your system’s limit. The -n switch limits the number of arguments to be passed to a given command. The xargs command will run continuously with n number of arguments until all arguments are exhausted.

传递给xargs的参数数量由系统的限制定义。 -n开关限制要传递给给定命令的参数数量。 xargs命令将连续运行n个参数,直到用尽所有参数。

The following example limits the number of arguments read from standard input to 2. Xargs command will go on reading two arguments at a time until it completes reading all of them. Further, the -t option will print the command in the standard output before executing it.

下面的示例将从标准输入中读取的参数数量限制为2。Xargs命令将继续一次读取两个参数,直到完成读取所有参数为止。 此外, -t选项将在执行命令之前在标准输出中打印该命令。

# echo "file1 file2 file3 file4 file5" | xargs -n 2 -t touch
Xargs Example 2

Xargs Example 2

Xargs示例2

执行前启用用户提示 (Enable User Prompt before execution)

The xargs with -p option will prompt user before execution of command. The following example shows xargs command prompts the user for either for ‘Y’ or ‘N’ before continuing with creating folders with each argument.

-p选项的xargs将在执行命令之前提示用户。 以下示例显示xargs命令提示用户输入'Y'或'N',然后继续使用每个参数创建文件夹。

# echo Hello World | xargs -p mkdir
# echo one two three | xargs -p -n 1 mkdir
Xargs Example 3

Xargs Example 3

Xargs示例3

使用find和xargs命令删除文件 (Delete files with find and xargs command)

One of the most common usages of xargs is to use it with the find command. Consider a situation where you want to remove or rename a bunch of files after running through find command. Here both find and xargs command can be used together on files those are matching certain attributes and then delete or rename them at one go.

xargs的最常见用法之一是将它与find命令一起使用。 考虑一种情况,您要在通过find命令运行后删除或重命名一堆文件。 在这里,find和xargs命令都可以在匹配某些属性的文件上一起使用,然后一次性删除或重命名它们。

For example, suppose you want to delete all the files in the /tmp folder those are created within the last five minutes. To do that using xargs, first, find out all the files those are matching with your chosen criteria by using find command and then pipe the output to the xargs command which in turn will use rm command to remove them.

例如,假设您要删除/tmp文件夹中所有在最近五分钟内创建的文件。 要使用xargs做到这一点,首先,使用find命令找出所有与所选条件匹配的文件,然后将输出通过管道传递到xargs命令,xargs命令随后将使用rm命令删除它们。

# find /tmp/* -cmin -5 
# find /tmp/* -cmin -5 | xargs -t rm
# find /tmp/* -cmin -5
Xargs Example 4

Xargs Example 4

Xargs示例4

使用xargs搜索模式 (Search for pattern using xargs)

Another powerful usages of Xargs command is to search for a pattern in a list of files returned by another unix command like ls or find. The following example combines find and xargs command wherein the find command returns all the text(.txt) files from current working directory. The result i.e. filenames are then piped to xargs command where we search for the word ‘file’ using grep "file". If the pattern is present in the files then the lines containing the pattern will be printed in the terminal along with the filename:

Xargs命令的另一个强大用法是在由另一个unix命令(如lsfind返回的文件列表中搜索模式。 下面的示例结合了findxargs命令,其中find命令返回当前工作目录中的所有text(.txt)文件。 结果,即文件名,然后通过管道传递到xargs命令,在此我们使用grep "file"搜索单词'file'。 如果文件中存在模式,则包含模式的行将与文件名一起打印在终端中:

# find . -name "*.txt" | xargs grep "file"
Xargs Example 5

Xargs Example 5

Xargs示例5

使用xargs运行多个命令 (Run multiple commands with xargs)

So far we have seen how xargs command uses piped arguments to do a various task like renaming a batch of files, searching for a pattern and so on. While doing these, it uses a single command after the piped operation. It is also possible to run multiple commands along with xargs.

到目前为止,我们已经了解了xargs命令如何使用管道参数来执行各种任务,例如重命名一批文件,搜索模式等等。 在执行这些操作时,它将在管道操作后使用单个命令。 还可以与xargs一起运行多个命令。

For example, if you want to create multiple files and list them at the same time using xargs command then run xargs command with -I switch followed by defining a replacement string. All occurrences of replacement string are then replaced with the argument passed to xargs.

例如,如果要创建多个文件并使用xargs命令同时列出它们,请使用-I开关运行xargs命令,然后定义替换字符串。 然后将所有出现的替换字符串替换为传递给xargs的参数。

# echo "file1 file2 file3" | xargs -t -I % sh -c '{ touch %; ls -l %; }'
Linux xargs command examples

Xargs Example 6

Xargs示例6

并行执行命令 (Executing commands in parallel)

In some situation, you might be using xargs to invoke a compute-intensive command for each argument. By taking advantage of your multi-core machine, it is possible to invoke the command in parallel. The -P switch exactly does this by defining the maximum number of processes. By default, the maximum number of process is 1.

在某些情况下,您可能正在使用xargs为每个参数调用计算密集型命令。 通过利用您的多核计算机,可以并行调用该命令。 -P开关通过定义最大进程数来精确地做到这一点。 默认情况下,最大进程数为1。

For example, the following xargs command prints the number 1 to 30 by taking 5 arguments at a time and most notably run up to 8 processes at a time.

例如,以下xargs命令通过一次采用5个参数来打印数字1至30,最值得注意的是一次运行多达8个进程。

# printf %s\\n {1..30} | xargs -n 5 -P 8
Xargs Example 7

Xargs Example 7

Xargs示例7

接受来自文件的输入 (Accept input from a file)

So far we have seen inputs for xargs command are read either from standard input or from the output of other commands through pipe operation. However, it is also possible for xargs command to read input from a file directly. The -a switch allows you to define a file for inputs needed by xargs.

到目前为止,我们已经看到xargs命令的输入是通过管道操作从标准输入或其他命令的输出中读取的。 但是,xargs命令也可以直接从文件读取输入。 -a开关允许您为xargs所需的输入定义文件。

The following xargs command read input from a file(input.txt) and then creates files using touch command.

以下xargs命令从文件(input.txt)读取输入,然后使用touch命令创建文件。

# cat input.txt
one.txt
two.txt
# xargs -a input.txt touch
Linux xargs command examples

Xargs Example 8

Xargs示例8

摘要 (Summary)

That’s all! Although we have covered a few basic usages of XARGS command you can now try out more advanced features of XARGS to build a powerful command. Hope you have liked the post and thanks for reading it.

就这样! 尽管我们已经介绍了XARGS命令的一些基本用法,但是您现在可以尝试XARGS的更多高级功能来构建功能强大的命令。 希望您喜欢这篇文章,并感谢您阅读。

翻译自: https://www.journaldev.com/29762/linux-xargs-command-examples

linux xargs命令

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值