Linux Command Learning xargs command

Source: Internet
Author: User

The Xargs command is a filter that passes parameters to other commands, and is also a tool for combining multiple commands. It specializes in translating standard input data into command-line arguments, Xargs can process pipelines or stdin and convert them into command parameters for specific commands. Xargs can also convert single-line or multiline text input to other formats, such as multiline variable-order lines and single-row multiple rows. The default command for Xargs is Echo, and the space is the default delimiter. This means that the input passed to Xargs through the pipeline will contain line breaks and whitespace, but with xargs processing, line breaks and whitespace will be replaced by spaces. Xargs is one of the important components of building single-line commands.

Args is used as a replacement tool to read the output of the input data after reformatting. Define a test file with multiple lines of text data:

Cat Test.txta b c d E F gh i j k l m no p q R S t u v w x y Z

Multiline input single-line output:

Cat Test.txt | Xargs a b c d e F g h i j k l m n o p q R S t u v w x y Z

-N option multi-line output:

Cat Test.txt | Xargs-N3 a B cd E F g h i j k l m n o p q R S t u v w x y z

The-D option allows you to customize a delimiter:

" Namexnamexnamexname " | XARGS-DX Name Name Name Name

Combine the-n option with:

" Namexnamexnamexname " | XARGS-DX-N2 Name Name Name Name

Read the stdin, pass the formatted parameter to the command, assuming a command for sk.sh and a file that holds the parameter arg.txt:



Echo $*

Arg.txt File Contents:

Cat Arg.txt AAA BBB CCC

An option of xargs-I, using-I to specify a replacement string {}, this string is replaced when the xargs extension, and when-I is used in conjunction with Xargs, each parameter command is executed once:

Cat Arg.txt | Xargs-i {}./sk.sh-p {}---PAAA-PBBB-P ccc-l

Copy all picture files to the/data/images directory:

LS *.jpg | Xargs-n1-i CP {}/data/images

Xargs combined with Find uses RM to delete too many files, you may get an error message:/BIN/RM Argument list too long. Use Xargs to avoid this problem:

" *.log " -print0 | Xargs-0 rm-f

xargs-0 the number of rows for all PHP files in a source code directory as a delimiter:

" *.php " -print0 | Xargs-0 wc-l

Find all the JPG files and compress them:

" *.jpg " -print | Xargs TAR-CZVF images.tar.gz

Xargs Other applications If you have a file that contains many URLs that you want to download, you can use Xargs to download all the links:

Cat Url-list.txt | Xargs wget-c
Child Shell (subshells)

Another command interpreter is started when you run a shell script. As if your command was interpreted at the command prompt, similar to a series of commands in a batch file. Each shell script effectively runs in a child process of the parent shell. This parent shell refers to the process of giving you a command designator in a control terminal or in a xterm window.

cmd1 | (CMD2; cmd3; cmd4) | Cmd5

If CMD2 is a CD/, then it will change the working directory of the child shell, this change is confined to the inside of the child shell, CMD5 is completely unaware of the changes in the working directory. A child shell is a sequence of commands that is embedded inside the parentheses (), and the variables defined inside the child shell are local variables. A child shell can be used to set temporary environment variables for a set of commands:

COMMAND1 COMMAND2 COMMAND3 (IFS=: PATH=/set -53  # Just exit from the child shell. # The parent shell is unaffected and the value of the variable has not changed. COMMAND6 COMMAND7

Transferred from: Http://man.linuxde.net/xargs

Linux Command Learning xargs command

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.