"Linux" parameter substitution command Xargs

Source: Internet
Author: User
Tags stdin

What is Xargs doing? In the literal sense, X is the multiplication sign of subtraction, and args is the meaning of arguments (parameter), so this thing is the meaning of the parameter that produces a command! Xargs can read stdin data and divide the stdin data into arguments with space or break characters as a resolution. Because it is delimited by the space character, so, if some file name or other meaning of the noun contains a space character, Xargs may be misjudged ~ His usage is actually quite simple! Just take a look at it first!

[Email protected] ~]#Xargs[-0EPN] command options and Parameters:-0: If the input stdin contains special characters, such as ', \, SPACEBAR, and so on, this-0The parameter can revert him to a normal character. This parameter can be used in a special state Oh! -E: This is EOF (end offile) means. Can be followed by a string whenXargswhen the string is parsed, it stops working! -P: When you run the argument of each command, you will be asked what the user means;-N: The number of times followed, the meaning of several parameters to be used each time the command is run. See example Three. WhenXargsWhen no commands are followed, the default is toEchoto do the output Oh! Example one: to/etc/passwdinside the first column out, take only three lines, use finger this command to show each account content [[email protected]~]#Cut-D':'-f1/etc/passwd|Head-N3|Xargsfingerlogin:root name:rootdirectory:/root Shell:/bin/Bashnever loggedinch. No Mail. No Plan ..... The finger account can be used to obtain a description of the accounts, such as the above output is the result of finger root#. In this example, we useCutRemove the account name and useHeadremove three accounts, and the last one isXargsChange the name of three accounts to the required parameters after finger! Example two: ditto, but every time you run finger, ask the user whether the action? [[Email protected]~]#Cut-D':'-f1/etc/passwd|Head-N3|Xargs-p fingerfinger Root bin daemon?... y ..... (omitted below) .... # hehe! This-P's option allows the user to use the process and be asked to see if each command is running! Example three: Will all the/etc/passwdthe accounts are finger, but only five accounts are consulted at a time [email protected]~]#Cut-D':'-f1/etc/passwd|Xargs-p-n5Fingerfinger Root bin Daemon admLP?... y ..... (omitted in middle) .... Finger UUCP operator Games GopherFTP?... y ..... (omitted at the bottom) .... # In here Brother Bird used-P This parameter allows you toN is more conceptual. In general, some commands can be followed by the arguments is limited, not unlimited accumulation, at this time, we can use-n# to help us divide the parameters into several parts, each of which is then run by command! That's OK! ^_^Example four: ibid, but when analyzed toLPTo end this sequence of orders? [[Email protected]~]#Cut-D':'-f1/etc/passwd|Xargs-p-e'LP'Fingerfinger Root bin Daemon adm?... # carefully compare it with the above case. Also note that the-E'LP'is connected, there is no space bar in the middle. # in the last example, the fifth parameter isLPAh, so we give E'LP'After the analysis to theLP# This string, the contents of the other stdin will beXargsAbandoned!

In fact, there are three or four small examples in man xargs, you can refer to the content yourself. In addition, Xargs really is very useful for a gadget! You really need to take a good look at the details! The reason for using Xargs is that many commands do not actually support pipeline commands, so we can provide the command by Xargs to reference standard input ! For example, we use the following example to illustrate:

Example five: Find the file name with special permissions under/sbin and usels-l list Detailed properties [[email protected]~]#Find/sbin-perm +7000|ls-l# results only the files in the directory where root is listed! That's not what we want! # because LL (lsis not the reason for the pipeline command! [[Email protected]~]#Find/sbin-perm +7000|Xargs ls-L-rwsr-xr-x1Root root70420May -   -/sbin/Mount. NFS-rwsr-xr-x1Root root70424May -   -/sbin/Mount. NFS4-rwxr-sr-x1Root root5920June the   -/sbin/netreport .... (omitted below) ....

  Reference:Http://vbird.dic.ksu.edu.tw/linux_basic/0320bash.php#xargs

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.