2023年7月26日发(作者:)
linuxxargs命令_Linuxxargs命令⽰例linux xargs命令The xargs command in Linux/UNIX reads items from the standard input, delimited by blanks or newlines, and executes thecommand one or more times with any initial / 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 doubleor single quotes or a backslash.标准输⼊上的空⽩⾏将被忽略。 这些xargs从标准输⼊读取的项⽬可以⽤双引号或单引号或反斜杠进⾏保护。The xargs command is extremely powerful, especially when combined with commands like or echo. In this tutorial, we willcover a few basic usages of xargs command to get you started with 命令⾮常强⼤,尤其是与或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 lista 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 outputfrom ls command is piped into the xargs command and the xargs command without any command echo the file list in thestandard input.不带任何命令的xargs命令将回显标准输⼊中的参数。 在下⾯的⽰例中,ls命令的输出通过管道传递到xargs命令中,⽽xargs命令在没有任何命令的情况下回显标准输⼊中的⽂件列表。# ls | xargsXargs Example ⽰例 following example shows how the output of echo command is piped to xargs and the xargs command uses the pipedargument to create folders using mkdir.以下⽰例显⽰了echo命令的输出如何通过管道传递给xargs,xargs命令如何使⽤管道参数使⽤mkdir创建⽂件夹。# echo 'one two three' | xargs mkdirXargs Example ⽰例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 ofarguments to be passed to a given command. The xargs command will run continuously with n number of arguments untilall 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 readingtwo arguments at a time until it completes reading all of them. Further, the -t option will print the command in the standardoutput before executing it.下⾯的⽰例将从标准输⼊中读取的参数数量限制为2。Xargs命令将继续⼀次读取两个参数,直到完成读取所有参数为⽌。 此外, -t选项将在执⾏命令之前在标准输出中打印该命令。# echo "file1 file2 file3 file4 file5" | xargs -n 2 -t touchXargs Example 2Xargs⽰例2执⾏前启⽤⽤户提⽰ (Enable User Prompt before execution)The xargs with -p option will prompt user before execution of command. The following example shows xargs commandprompts 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 mkdirXargs Example 3Xargs⽰例3使⽤find和xargs命令删除⽂件 (Delete files with find and xargs command)One of the most common usages of xargs is to use it with the . Consider a situation where you want to remove or rename abunch of files after running through find command. Here both find and xargs command can be used together on files thoseare matching certain attributes and then delete or rename them at one 的最常见⽤法之⼀是将它与⼀起使⽤。 考虑⼀种情况,您要在通过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 dothat using xargs, first, find out all the files those are matching with your chosen criteria by using find command and thenpipe 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 -5Xargs Example 4Xargs⽰例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 commandlike 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 filenames are then piped to xargs command where we search for theword ‘file’ using grep "file". If the pattern is present in the files then the lines containing the pattern will be printed in theterminal along with the filename:Xargs命令的另⼀个强⼤⽤法是在由另⼀个unix命令(如ls或find返回的⽂件列表中搜索模式。 下⾯的⽰例结合了find和xargs命令,其中find命令返回当前⼯作⽬录中的所有text(.txt)⽂件。 结果,即⽂件名,然后通过管道传递到xargs命令,在此我们使⽤grep "file"搜索单词'file'。 如果⽂件中存在模式,则包含模式的⾏将与⽂件名⼀起打印在终端中:# find . -name "*.txt" | xargs grep "file"Xargs Example 5Xargs⽰例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 possibleto 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 xargscommand with -I switch followed by defining a replacement string. All occurrences of replacement string are then replacedwith the argument passed to xargs.例如,如果要创建多个⽂件并使⽤xargs命令同时列出它们,请使⽤-I开关运⾏xargs命令,然后定义替换字符串。 然后将所有出现的替换字符串替换为传递给xargs的参数。# echo "file1 file2 file3" | xargs -t -I % sh -c '{ touch %; ls -l %; }'Xargs Example 6Xargs⽰例6并⾏执⾏命令 (Executing commands in parallel)In some situation, you might be using xargs to invoke a compute-intensive command for each argument. By takingadvantage of your multi-core machine, it is possible to invoke the command in parallel. The -P switch exactly does this bydefining 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 runup to 8 processes at a time.例如,以下xargs命令通过⼀次采⽤5个参数来打印数字1⾄30,最值得注意的是⼀次运⾏多达8个进程。# printf %sn {1..30} | xargs -n 5 -P 8Xargs Example 7Xargs⽰例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 commandsthrough pipe operation. However, it is also possible for xargs command to read input from a file directly. The -a switchallows you to define a file for inputs needed by xargs.到⽬前为⽌,我们已经看到xargs命令的输⼊是通过管道操作从标准输⼊或其他命令的输出中读取的。 但是,xargs命令也可以直接从⽂件读取输⼊。 -a开关允许您为xargs所需的输⼊定义⽂件。The following xargs command read input from a file() and then creates files using touch command.以下xargs命令从⽂件()读取输⼊,然后使⽤touch命令创建⽂件。# cat # xargs -a touchXargs Example 8Xargs⽰例8摘要 (Summary)That’s all! Although we have covered a few basic usages of XARGS command you can now try out more advancedfeatures of XARGS to build a powerful command. Hope you have liked the post and thanks for reading it.就这样! 尽管我们已经介绍了XARGS命令的⼀些基本⽤法,但是您现在可以尝试XARGS的更多⾼级功能来构建功能强⼤的命令。 希望您喜欢这篇⽂章,并感谢您阅读。linux xargs命令
发布者:admin,转转请注明出处:http://www.yc00.com/web/1690365041a338701.html
评论列表(0条)