Xargs command details, linuxxargs command details

Source: Internet
Author: User

Xargs command details, linuxxargs command details

The xargs Command Re-formats the received data and provides it as a parameter to other commands. The following describes the xargs command usage techniques.

1. Convert multiple rows into single rows:

[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 row input to multiple rows output:

[root@host1 test]# cat example.txt | xargs -n 31 2 34 5 67 8 910 11 12

Convert custom delimiters (the default delimiters are spaces ):

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

Ii. Use in scripts:

[Root @ host1 test] # cat echo. sh #! /Bin/bashecho $ * '^-^' when the parameter is passed to echo. sh, it prints out these parameters and ends 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 our example, we put all the sources in the args.txt file, but in addition to these parameters, we also need some fixed parameters, such:

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

During the preceding command execution, Tom is a variable and the rest is a constant. We can extract parameters from "args.txt" and provide them to the command in the following way:

[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

In this case, we need to use the-I command in xargs:

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

-I {} specifies the replacement string. For each command parameter, the string {} will be replaced by the parameter read from stdin,

When-I is used, the command is executed cyclically. If there are four parameters, the command will be executed four times together, in each execution, {} is replaced with the corresponding parameter.

3. Use it in combination with find

Xargs and find are a very good combination, but we usually use them in a wrong way, such:

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

This operation is dangerous. Sometimes, files that do not need to be deleted are deleted. If the file name contains a space character (''), xargs may think of them as delimiters (for example, file text.txtwill be recognized by xargsas fileand text.txt by mistake ).

If we want to use the find output as the xargs input, we must use-print0 and find in combination to separate the output with the character null ('\ 0' separator, use findto find all. txt files, and then use xargs to delete these files:

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

In this example, all the. txt files can be deleted. xargs-0 uses \ 0 as the input delimiter.

4. Use the while statement and sub-shell

[Root @ host1 test] # cat files.txt | (while read arg; do cat $ arg; done) this command is equivalent: [root @ host1 test] # cat files.txt | xargs-I {} cat {}

In the while LOOP, you can replace cat $ arg with any number of commands, so that we can execute multiple commands for the same parameter, or do not use pipelines, pass the output to other commands. This technique is applicable to multiple problem scenarios. Multiple commands in the sub-shell operator can be run as a whole.

  

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.