Linux Xargs Commands

Source: Internet
Author: User
Tags echo command

[Reprint] Reference blog: https://www.cnblogs.com/wangqiguo/p/6464234.html

echo '--help ' | Cat This command outputs the echo content, which means that Echo's content is treated as a cat-processed file, in effect the output of the Echo command is directed to the cat input through the pipeline. Then cat reads the text content to be processed from its standard input. This is equivalent to having a line character '--help ' in the Test.txt file and then running the cat test.txt effect.

and Echo '--help ' | Xargs cat is equivalent to cat--help what does it mean that Xargs takes its accepted string--help into a cat command parameter to run the cat command, likewise echo ' test.c test.cpp ' | Xargs Cat is equivalent to the cat test.c test.cpp the contents of test.c and Test.cpp are displayed at this time.

1.-d option
By default, Xargs divides the contents of its standard input into a number of blanks (including spaces, tabs, carriage returns, and so on) and runs it as a command-line argument after it is passed to it, and we can use the-d command to specify delimiters, for example:
Echo ' [ Email protected]@33 ' | Xargs echo 
Output:
[email protected]@33
separated by default, then [email  protected]@33 there is no whitespace in this string, so it is actually equivalent to echo [email protected]@33 where the string ' [email protected]@33 ' A command-line argument that is treated as an echo command

Echo ' [email protected]@33 ' | xargs-d ' @ ' echo Output:
One-by-one
specifies that the parameter is split with the @ symbol, so the equivalent of Echo 11 22 33 is equivalent to passing 3 parameters to echo, 11, 22, and three

2.-P option
With this option, Xargs does not immediately execute the commands that follow it, but instead outputs the complete command to be executed (including commands and command-line arguments that are passed to the command), asks whether to execute, enters Y to continue, or does not execute. This way you can see clearly what the command is executing, that is, what the arguments Xargs pass to the command, such as:
Echo ' [Email protected]@33 ' | Xargs-p-d ' @ ' echo
Output:
Echo 11 22 33
?... y ==> here to ask whether to execute the command echo 11 22 33 Enter Y and return, the execution result is displayed, otherwise do not execute
==> Execution Results

3.-N option
This option represents the command-line arguments that will be generated by Xargs, each time passing several arguments to the command following it, such as if Xargs reads the contents from standard input, and then splits the command-line arguments after the delimiter is divided by 10, the command that is passed to Xargs after using-N 3 is 3 arguments , because there are 10 parameters, it takes 4 times to run out of parameters. For example:

Echo ' [email protected]@[email protected]@[email protected]@[email protected]@[email protected] ' | Xargs-d ' @ '-N 3 echo
Output Result:
11 22 33
44 55 66
77 88 99
00
Equivalent to:
Echo 11 22 33
Echo 44 55 66
Echo 77 88 99
echo 00
It actually runs 4 times, passes 3 parameters at a time, and ends with one, passing a parameter directly.

4.-e option, some xargs versions of the system may be e-eof-str
This option specifies a string that, when xargs resolves multiple command-line arguments, searches to the command-line arguments that are specified by-e, and passes only the arguments preceding the command-line parameter specified by-e (which does not include the parameter specified by-e) to the command following Xargs
Echo ' 11 22 33 ' | Xargs-e ' echo '
Output:
11 22

You can see that normally there are 3 command line arguments 11, 22, 33 because the use of-e ' 33 ' means that the arguments before the command line parameter 33 are passed to the executed command, 33 itself is not passed. Equivalent to echo 11 22 here-e actually has the function of searching, indicating that only the command line arguments that are read by Xargs are taken to the command execution.

Note:-E is valid only if Xargs does not specify-D, and does not work if the-D is specified, regardless of what character is specified by-D, and no space.

Echo ' 11 22 33 ' | Xargs-d '-e ' echo + output 11 22 33
Echo ' [email protected]@[email protected]@[email protected]@[email protected]@[email protected] AA BB ' | Xargs-e '-d ' @ '-p echo + output one by one all

# #-0 option means "\" as a delimiter, commonly used in conjunction with find

Find. -name "*.txt"
Output:
./2.txt
./3.txt
./1.txt = By default The result of find is a newline followed by each record, that is, each record is a new row

Find. -name "*.txt"-print0
Output:
The./2.txt./3.txt./1.txt = plus-print0 parameter means that each result of the find output is followed by '% ' instead of newline

Find. -name "*.txt"-print0 | xargs-0 Echo
Output:
./2.txt./3.txt./1.txt

Find. -name "*.txt"-print0 | Xargs-d ' + ' echo
Output:
./2.txt./3.txt./1.txt

Xargs-0 and-d ' + ' means that the content it reads from the standard input is split using ' \ s ', because the result of find is delimited with ' \ s ', so Xargs uses '% ' to separate the results of Find by 3 parameters:./2.txt./3.txt /1.txt Note that there are spaces in the middle. The above results are equivalent to echo./2.txt./3.txt./1.txt

Actually using the Xargs default Whitespace delimiter is also available for find. -name "*.txt" | Xargs echo because newline characters are also one of the default whitespace characters for Xargs. The Find command does add a newline after each string without-print0 its search results

Linux Xargs Commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.