1, effect :

(1) Pass the standard output of the previous command to the next , As its parameter ,xargs The default command for is echo, Space is the default delimiter

(2) Convert multiline input to single line

2, Usage mode :front command | xargs -option later command

front command: Previous command

-option: xargs Options for

later command: Next command

3,xargs Common options

-n: Specify the number of parameters to process at a time

-d: Custom parameter delimiter

-p: Ask if you want to run later command parameter

-t : Indicates print first command , And then

-i : Item by item

... More parameters view man xargs

4, Use case

Test text :xargs.txt

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

(1) Multi line input single line output

cat xargs.txt | xargs

( 2) Specify the number of parameters to process at a time : Designated as 5, Multiline output

cat xargs.txt | xargs -n 5

(3) Custom parameter delimiter : Specify the delimiter as 'm'

cat xargs.txt | xargs -d m

(4) Ask if you want to run later command parameter

cat xargs.txt | xargs -n 5 -p

(5) Rename all files , Process each parameter item by item

ls *.txt |xargs -t -i mv {} {}.bak

More parameter options man xargs

5,xargs And pipeline | The difference between

| Used to pass the standard output of the previous command to the standard input of the next command ,xargs Pass the standard output of the previous command to the next , As its parameter .

# Use pipes to ls The results of ,ls The result of the standard output as cat Standard output of ls | cat
# use xargs take ls As a result of cat Parameters of ,ls The result of is the file name , therefore cat File name is to view the contents of the file ls | xargs cat

6,xargs And exec The difference between

(1)exec Parameters are passed one by one , Pass a parameter to execute the command once ;xargs Pass parameters to commands at once , have access to -n Number of control parameters
#xargs Pass parameters once to echo, I.e. implementation :echo begin ./xargs.txt ./args.txt find . -name '*.txt'
-type f | xargs echo begin #exec Pass one parameter at a time , I.e. implementation :echo begin ./xargs.txt;echo begin
./args.txt find . -name '*.txt' -type f -exec echo begin {} \;

(2)exec File names can also be processed with special characters such as spaces ;xargs Cannot process special file names , Special handling is required if you want to handle special filenames

#find The filename after contains spaces find . -name '*.txt' -type f | xargs cat find . -name '*.txt'
-type f -exec cat {} \;

reason : By default , find Every output file name , Followed by a line break ('\n'), So what we see find The output of is line by line ,xargs
The default is to use blank characters ( Space , TAB, Line break ) To split the records , So the filename ./t t.txt It's interpreted as two records ./t and t.txt,
cat These two files cannot be found , So it's a mistake , To solve such problems ,   Give Way find Print out a filename followed by a NULL character ('') Instead of line breaks , And then tell me
xargs Also used NULL Character as a separator for records , Namely find -print0 and xargs -0 , So you can handle special filenames .#xargs Handle special file names
find . -name '*.txt' -type f -print0 | xargs -0 cat

Technology
©2019-2020 Toolsou All rights reserved,
Centos7 lower mysql8.0 To install and change the initial password ;JQ get request Splicing url parameter ( query criteria )415 Status code to background error mybatis-config.xml Set default value configuration Three methods of value transfer between non parent and child components airflow Question series 2 —— task keep running Suspended animation @Repository The role of annotations STM32 Infrared tracking car [AndroidO] [RK3399] -- GPIO Drive and control mode about Bellman-Ford Personal understanding of algorithms