Xargs command details, linuxxargs command

Source: Internet
Author: User

Xargs command details, linuxxargs command

Xargs is a filter for passing parameters to commands and a tool for combining multiple commands. It divides a data stream into small enough blocks to facilitate processing by filters and commands. In general, xargs reads data from an MPS queue or stdin, but it can also read data from the file output. The default xargs command is echo, which means that the input passed to xargs through the pipeline will contain line breaks and white spaces. However, after xargs is processed, line breaks and white spaces will be replaced by spaces.


1. Option explanation

-0 when sdtin contains special characters, it is treated as a general character and may contain spaces.
For example, root @ localhost :~ /Test # echo "//" | xargs echo
Root @ localhost :~ /Test # echo "//" | xargs-0 echo
/
-A file is read from the file as sdtin


-E flag note that sometimes it may be-E, and the flag must be a sign separated by spaces. When xargs analyzes the flag, it stops.


-P: Ask the user once each time an argument is executed.


-N num is followed by the number of times, indicating the number of argument used at a time when the command is executed. By default, all argument values are used.


-T indicates that the command is printed and then executed.


-I or-I assigns each item name of xargs to {} in a row, which can be replaced.


-R no-run-if-empty: When the xargs input is empty, xargs is stopped and no execution is required.


-S num refers to the maximum number of characters in the command line after xargs.


-L num Use at most max-lines nonblank input lines per command line.-s contains spaces.


-L same as-L


-D delim delimiter the default xargs separator is carriage return, and argument separator is space. Here the xargs separator is modified.


-X exit is mainly used in combination with-s.


-P: the maximum number of processes to be modified. The default value is 1. If the value is 0, it is as much as it can. In this example, I didn't think it should be useless at ordinary times.

2. Instance
Xargs is a powerful command that can capture the output of a command and then pass it to another command. Below are some practical examples of how to effectively use xargs.

1. When you try to delete too many files with rm, you may get an error message:/bin/rm Argument list too long. Use xargs to avoid this problem.
Find ~ -Name '*. log'-print0 | xargs-0 rm-f

2. obtain all * under/etc *. there are several different methods to get the same results for the file list at the end of conf. The following example only demonstrates how to use xargs, in this example, xargs is used to pass the output of the find command to ls-l.
# Find/etc-name "*. conf" | xargs ls-l

3. If a file contains many URLs you want to download, you can use xargs to download all links.
# Cat url-list.txt | xargs wget-c

4. Search for all jpg files and compress them.
# Find/-name *. jpg-type f-print | xargs tar-cvzf images.tar.gz

5. Copy all image files to an external hard drive.

# Ls *. jpg | xargs-n1-I cp {}/external-hard-drive/directory

6.-I command

$ Ls | xargs-t-I mv {}{}. bak
-The I option tells xargs to replace {} with the name of each item {}. The-t option indicates that xargs prints the command before executing it.

3. Description
1. Many commands are not supported. | you can use xargs to pass Parameters in pipelines.
How does xargs use the linux Command awk?

These commands are widely used, such as AWK. I think they can all be called a language. The following is a small example:
Root @ Bizbox :~ # Echo $ {WHEN: = "Saturday "}
SS
Root @ Bizbox :~ # Echo "chen | wei | sun" | awk-F "|" '{print $1 }'
Chen
Root @ Bizbox :~ # Echo "chen | wei | sun" | awk-F "|" '{print $2 }'
Wei
Root @ Bizbox :~ # Echo "chen | wei | sun" | awk-F "|" '{print $3 }'
Sun
Root @ Bizbox :~ #

In linux, use the find and xargs commands to delete 10 files.

Find.> list | tail-10 list | xargs rm-rf {}| xargs rm-rf list

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.