Linux Shell Command-xargs

Source: Internet
Author: User
1 Overview

Xargs is a common command for UNIX and Unix-like operating systems. It is used to convert the parameter list into small segments and pass it to other commands to avoid the problem that the parameter list is too long.

2 features

(1) process space in the file/directory name

Find.-name '*~ '-Print0 | xargs-0 RM has a problem. If the file or directory name contains spaces, there may be some problems, because xargs will divide the input according to the blank space by default. A simple solution is to tell find to use NUL (\ 0) to split the result (by providing the-print0 option to find ), it also tells xargs to use NUL to separate input (-0 ).

Delete the backup file, even if it contains spaces: find.-Name '*~ '-Print0 | xargs-0 RM

(2) When the-I option is used, {} can be used to replace each parameter.

Use the-I option. By default, {} is used to replace the previously transmitted parameters. However, the length of a parameter is limited. Therefore, you may need to use the-s option to increase the length of a parameter.

The default length is 4096.

Somecommand | xargs-s 50000 echo | xargs-I '{}'-s 100000 RM '{}'

You can use the-I option to specify parameters (do not use the-I default {})

Ls-L | xargs-I hahha echo hahha_123455666
Total 2128_123455666
-RW-r --. 1 algo 13420 Aug 20 all. Records _123455666


(3) number of specified parameters

If the command you want to execute only accepts one or two parameters, for example, using the diff command to compare two files, the-n option of xargs will be very useful, it can specify several parameters for the target command at a time. If the number of parameters is greater than the number you set, the command will be repeatedly called until all input has been executed. Note that the number of parameters called at the last time may be less than the specified number. Let's take a simple example:

Echo {0 .. 9} | xargs-N 2
0 1
2 3
4 5
6 7
8 9

(4) number of specified parameter rows

Similarly, you can use the-l parameter to specify only a few rows of input at a time, for example,-l 1 transfers a line from the input every time to the command to be executed as a parameter. Of course, you can change 1 to any line, but 1 is the most commonly used, the following command demonstrates how to get code changes for each git commit:

Git log -- format = "% H % P" | xargs-L 1 git diff

(5) parallel

Parallel sleep

$ Time ECHO {1 .. 5} | xargs-N 1-P 5 sleep

Real 0m5. 013 suser 0m0. 003 SSYS 0m0. 014 s

Linear sleep

$ Time ECHO {1 .. 5} | xargs-N 1 sleep

Real 0m15. 022 suser 0m0. 004 SSYS 0m0. 015 s

If you are interested in using xargs for parallel computing, you should probably look at GNU parallel. In comparison, the advantage of xargs is that most systems support it by default, it can also be easily installed on BSD and OS X, but parallel has many outstanding features.

3. Examples common applications (1) Use Find and xargs in combination to perform some operations on the list of objects returned by FIND ., Find is a very powerful command. It also has parameters such as-exec and-delete that can be used to execute commands and delete files. However, if you are not familiar with the find options, or cannot think of the odd syntax-exec, you can still use simpler xargs, and xargs is more efficient.

In this example, xargs splits the long string file list generated by find into multiple substrings and calls RM for each substring. For example, it is much more efficient to use the find command.

find /path -type f -exec rm '{}' \;



Reference: http://heikezhi.com/yuanyi/things-you-didnt-know-about-xargs

"Things
You (probably) didn't know about xargs ", by Cosmin
Stejerean

Related Article

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.