Linux output conversion command xargs

1. Basic usage

xargsThe function of the command is to convert the standard input into command line parameters.

Reason: Most commands do not accept standard input as parameters, and can only enter parameters directly on the command line, which makes it impossible to pass parameters with pipeline commands

For example, echo does not accept standard output as a parameter, and xargs can be used for conversion:

$ echo "hello world" | xargs echo
hello world

2. Parameters

-dspecify delimiter

By default, xargsnewlines and spaces are used as delimiters to break up standard input into individual command-line arguments.

$ echo "one two three" | xargs mkdir

In the above code, mkdirthree subdirectories will be created and executed mkdir one two three.

-dParameters can change the delimiter

$ echo -e "a\tb\tc" | xargs -d "\t" echo
a b c

The above command specifies tab \tas the delimiter, so a\tb\tcthat translates to three command line arguments. echoThe command's -eargument indicates the interpretation of escape characters.

-p -tprint the command to be executed

-pThe parameter prints out the command to be executed and asks the user if they want to execute it.

$ echo 'one two three' | xargs -p touch
touch one two three ?...

-tThe parameter is to print out the final command to be executed, and then execute it directly without user confirmation.

$ echo 'one two three' | xargs -t rm
rm one two three

-Ipass parameter alias

If xargsyou want to pass command-line arguments to multiple commands, you can use -Iparameters. [It seems that the parameters will be divided by space or carriage return, and then the command will be executed repeatedly, instead of being treated as multiple parameters of the command]

-ISpecify a replacement string for each command-line argument.

$ cat foo.txt
one
two
three

$ cat foo.txt | xargs -I file sh -c 'echo file; mkdir file'
one 
two
three

$ ls 
one two three

In the above code, foo.txtit is a three-line text file. echoWe want to execute two commands ( and mkdir) for each command-line argument , using the substitution string -I fileindicating that it is a command-line argument. fileWhen executing the command, the specific parameters will replace echo file; mkdir filethe two inside file.

-l -LSpecify how many lines to use as a command line argument

$ echo -e "a\nb\nc" | xargs -L 1 echo
a
b
c

-nSpecify multiple items on a line as a command-line argument

$ echo {
    
    0..9} | xargs -n 2 echo
0 1
2 3
4 5
6 7
8 9

--max-procsmulti-threaded execution

xargsBy default, only one process is used to execute commands. If the command needs to be executed multiple times, it must wait for the previous execution to finish before executing the next one.

--max-procsThe parameter specifies how many processes to execute the command in parallel at the same time. --max-procs 2It means that at most two processes can be used at the same time, --max-procs 0and it means that the number of processes is not limited.

$ docker ps -q | xargs -n 1 --max-procs 0 docker kill

The above command means to close as many Docker containers as possible at the same time, so that the running speed will be much faster

Guess you like

Origin blog.csdn.net/shuofxz/article/details/128045333