Tips for using the xargs command in linux

  • 2020-06-07 05:56:00
  • OfStack

preface

The xargs command reformats the received data and provides it as a parameter to other commands. Here are some tips for using the xargs command.

1. Convert multi-line input into single-line input:


[root@host1 test]# echo -e "1 2 3 4 5 \n6 7 8 \n9 10 11 12" >example.txt
[root@host1 test]# cat example.txt 
1 2 3 4 5 
6 7 8 
9 10 11 12
[root@host1 test]# cat example.txt |xargs 
1 2 3 4 5 6 7 8 9 10 11 12 

Convert a single line of input into multiple lines of output:


[root@host1 test]# cat example.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
10 11 12 

Custom delimiters for conversion (default delimiters are Spaces) :


[root@host1 test]# echo "Hello:Hello:Hello:Hello" | xargs -d : -n 2
Hello Hello
Hello Hello 

2. Use:


[root@host1 test]# cat echo.sh 
#!/bin/bash
echo $* '^-^'

When parameters are passed to echo.sh It will print out the parameters and end with "^-^" :


[root@host1 test]# echo -e "Tom\nHarry\nJerry\nLucy" > args.txt
[root@host1 test]# cat args.txt | xargs bash echo.sh 
Tom Harry Jerry Lucy ^-^
[root@host1 test]# cat args.txt | xargs -n 2 bash echo.sh 
Tom Harry ^-^
Jerry Lucy ^-^ 

In the above example, we put all the parameter sources into the args.txt file, but in addition to these parameters, we also need 1 fixed parameter, such as:


[root@host1 test]# bash echo.sh Welcome Tom 
Welcome Tom ^-^ 

During the above command execution, Tom is a variable and the rest is a constant. We can extract the parameters from "args.txt" and provide them to the command as follows:


[root@host1 test]# bash echo.sh Welcome Tom 
[root@host1 test]# bash echo.sh Welcome Herry
[root@host1 test]# bash echo.sh Welcome Jerry
[root@host1 test]# bash echo.sh Welcome Lucy 

At this point, we need to use the -ES35en command in xargs:


[root@host1 test]# cat args.txt | xargs -I {} bash echo.sh Welcome {} 
Welcome Tom ^-^
Welcome Harry ^-^
Welcome Jerry ^-^
Welcome Lucy ^-^ 

-ES39en {} specifies the replacement string. For each command argument, the string {} is replaced by the argument read from stdin,

With -ES43en, the command is executed in a loop. If there are four arguments, the command is executed four times with {}1 and the {} is replaced with the corresponding argument in each execution.

3. Use in combination with find

xargs and find are a great pair, but we usually use them in the wrong way, such as:


[root@host1 test]# find . -type f -name "*.txt" -print | xargs rm -f 

This can be dangerous, sometimes deleting files that do not need to be deleted, and if the file name contains a space character (' '), xargs will probably think they are delimited (for example, file text.txt is mistaken for file and text.txt).

If we want the output of find to be the input of xargs, we must separate the output with -ES66en0 and find using the character null ('\0'), find all the files of.txt with find, and then delete them with xargs:


[root@host1 test]# find . -type f -name "*.txt" -print0 | xargs -0 rm -f 

So you can delete all the.txt files, xargs -0 Use \0 as the input delimiter.

4. Use while statement and sub-ES80en


[root@host1 test]# cat example.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
10 11 12 
0

This command is equivalent to:


[root@host1 test]# cat example.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
10 11 12 
1

In the while loop, you can put cat $arg Replace it with any number of commands so that you can execute multiple commands for the same argument, or pass output to other commands without the help of a pipe, a technique that works well in a variety of problem scenarios. Multiple commands within the child shell operator can be run as one unit.

conclusion


Related articles: