Overview
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.
Grammar
[Email protected] ~]#Xargs--Helpusage:Xargs[ -0prtx] [--interactive] [--NULL] [-d|--delimiter=Delim] [-e Eof-str] [-e[eof-str]] [--eof[=eof-STR]] [-L max-lines] [-l[max-lines]] [--max-lines[=max-Lines]] [-I replace-str] [-i[replace-str]] [--replace[=replace-STR]] [-N Max-args] [--max-args=max-args] [-S max-chars] [--max-chars=max-chars] [-P Max-procs] [--max-procs=max-procs] [--show-Limits] [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file] [--version] [--help] [command [initial-arguments]] Report Bugs to<[email Protected]>.
Chestnuts
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:
Cat Xargs w x y z
Multiline input single-line output
Cat Xargs wcatxargs. txt | Xargs w x y z
-N option multi-line output
Cat Xargs xargs -W
The-D option allows you to customize a delimiter:
Echo " Namexnamexnamexname " xargs -dxname name name Name
Combined with the-n option
Echo " Namexnamexnamexname " xargs -dx-n2name namename name
Read stdin, pass formatted parameters to command
Suppose a command is xgj.sh and a file that holds parameters Args.txt:
Args.txt already has Execute permissions
Cat Xgj. SH #!/bin/bash# Print all the parameters echo $*cat args.txt AAABBBCCC
One of the options for Xargs- I.,
Use-I to specify a replacement string {},
This string is replaced when the Xargs is extended, and when-I is used in conjunction with Xargs, each parameter command is executed once:
Cat Xargs -i {}./xgj. SH XXX {} yyyxxx AAA yyyxxx BBB yyyxxx CCC YYY
Copy all picture files to the/data/images directory:
ls Xargs CP
Xargs combined with Find use
When you delete too many files with RM, you may get an error message:
/bin/rmlong.
Use Xargs to avoid this problem:
Find " *.log " Xargs -0RM
The xargs-0 is used as a delimiter.
Count the number of rows for all the py files in a source code directory:
Find " *.py " Xargs -0WC
Find all the JPG files and compress them:
Find " *.jpg " Xargs tar -czvf images. Tar
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
Linux-xargs Command