"Go" "Linux" Linux under xargs command

Source: Internet
Author: User
Tags stdin

Xargs command
Current Location: Home? Software/printing/development/tools? Xargs xargs Command Common Tools command 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.

Xargs command Usage

The Xargs 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:

Catw x y z

Multiline input single-line output:

Cat Xargs  w x y z

-N option multi-line output:

Cat xargs -w  xy z

The-D option allows you to customize a delimiter:

Echo " Namexnamexnamexname " xargs -dX name name Name Name

Combine the-n option with:

Echo " Namexnamexnamexname " xargs -dx-n2 name NameName name

Read stdin, pass formatted parameters to command

Suppose a command is sk.sh and a file that holds parameters Arg.txt:

#!/bin/echo $*

Arg.txt File Contents:

Cat arg.txt AAABBBCCC

An optionof 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 Xargs -i {}./sksh -p {}-P AAA-P BBB-P ccc-l

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

ls Xargs CP {}/data/images

Xargs combined with Find use

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

Find " *.log " Xargs -0rm -F

The xargs-0 is used as a delimiter.

Count the number of rows in a source code directory for all PHP files:

Find " *.php " Xargs -0WC -L

Find all the JPG files and compress them:

Find " *.jpg " Xargs tar -czvf images. tar. gz

Xargs Other applications

If you have a file that contains many URLs that you would like to download, you can download all the links using Xargs:

Cat 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 command2command3 (    IFS=:    PATH=/bin    unset TERMINFO    -     shift5    COMMAND4    COMMAND5    3  # Just quit from the child shell. # The parent shell is unaffected and the value of the variable has not changed. COMMAND6 COMMAND7

Original address: Http://man.linuxde.net/xargs

"Go" "Linux" Linux under xargs command

Related Article

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.