The Xargs of the Find command

Source: Internet
Author: User
Tags stdin

Xargs usage Explanation

1. Introduction

The key to using this command is that many commands do not support | Pipelines to pass parameters, and in daily work there is a need for the Xargs command, for example:

Find/sbin-perm +700 |ls-l This command is wrong.

find/sbin-perm +700 |xargs ls-l This is the right thing.

Xargs can read into the stdin data, and with blank characters or word-breaking characters as a resolution, the stdin data is separated into arguments. Because it is separated by a blank character, if there are some names or other meanings of nouns containing empty white space, Xargs may be misjudged ~ His usage is actually also full of simple. Just take a look at the first.

2. Option Explanation

-0 when Sdtin contains special characters, think of it as a general character, like/' spaces, etc.

Example: Root@localhost:~/test#echo "//" |xargs Echo

Root@localhost:~/test#echo "//" |xargs-0 Echo

/

-A file is read from files as Sdtin, (see example I)

- e flag , note that sometimes-e,flag must be a space-delimited flag that stops when Xargs analyzes the flag that contains flag. (Example II)

- p asks the user once every time a argument is executed. (Example III)

-N num followed by the number of times, indicating the number of times the command was executed at the time of execution, argument by default. (Example IV)

- T means that the command is printed before execution. (Example V)

-I or-I, which depends on Linux support, will be the xargs of each name, usually a line assigned to {}, you can use {} instead. (Example VI)

- R no-run-if-empty stop Xargs when the input of Xargs is empty, no more execution. (Example VII)

The best number of characters for the- s num command line, which refers to the maximum number of command-line characters for the command after Xargs. (Example VIII)

-L num use on most max-lines nonblank input lines per command line.-s is a space.

-L and-l

The- D delim delimiter, the default Xargs delimiter is carriage return, the delimiter for argument is a space, and here the Xargs delimiter is modified (example nine)

- x exit means mainly with-s used.

-P Modify the maximum number of processes, the default is 1, for the 0 time as many as it can, this example I did not think, it should not be used normally



Example one:

root@localhost:~/test#Cat Test

#!/bin/sh

echo "Hello world/n"

root@localhost:~/test#xargs-a Test echo

#!/bin/sh echo Hello world/n

root@localhost:~/test#

Example two:

root@localhost:~/test#Cat txt

/bin Tao Shou Kun

root@localhost:~/test#cat txt|xargs-e ' Shou ' echo

/bin Tao

root@localhost:~/test#

Example three:

root@localhost:~/test#cat Txt|xargs-p Echo

Echo/bin Tao Shou kun ff?... y

/bin Tao Shou Kun ff

Example four:

root@localhost:~/test#cat txt|xargs-n1 Echo

/bin

Tao

Shou

Kun

root@localhost:~/test3#cat Txt|xargs Echo

/bin Tao Shou Kun

Example five:

root@localhost:~/test#cat txt|xargs-t Echo

Echo/bin Tao Shou Kun

/bin Tao Shou Kun

Example VI:

$ ls | XARGS-T-i mv {} {}.bak

Example VII:

root@localhost:~/test#echo "" |xargs-t MV

Mv

mv:missing file operand

Try ' MV--help ' for more information.

root@localhost:~/test#echo "" |xargs-t-R MV

root@localhost:~/test#

(Direct exit)

Example VIII:

root@localhost:~/test#cat Test |xargs-i-x-s echo "{}"

Exp1

Exp5

File

Xargs:argument Line too long

Linux-2

root@localhost:~/test#

Example nine:

root@localhost:~/test#Cat txt |xargs-i-p echo {}

Echo/bin Tao Shou kun?... y

root@localhost:~/test#cat txt |xargs-i-p-d "" Echo {}

Echo/bin?... y

Echo Tao?.../bin

Y

Echo Shou?... tao

Again such as:

root@localhost:~/test#cat test |xargs-i-p-d "" Echo {}

Echo EXP1

Exp5

File

Linux-2

Ngis_post

Tao

Test

Txt

Xen-3

?... y

root@localhost:~/test#Cat Test |xargs-i-P echo {}

echo Exp1?... y

echo EXP5?... EXP1

Y

echo File?... exp5

Y






Cat Example.txt
1 2 3) 4 5
6 7
8


Cat Example.txt | Xargs
1 2 3 4 5 6 7 8


Cat Example.txt | Xargs-n 2
1 2
3 4
5 6
7 8

Whitespace is the default delimiter,-n means that several parameters are displayed per line

You can also use the-d parameter to separate parameters, as follows:


echo "Splitxhixamoslixsplit" | Xargs-d "X"-N 1
Split
Hi
Amosli
Split




When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.

The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.

In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there will be too many processes and degraded system performance, so the efficiency is not high; With the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.

usage Examples:

Example 1: find each normal file in your system, and then use the Xargs command to test what type of file they belong to

Life

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.