Linux xargs Command, application of-print0,xargs-0

Source: Internet
Author: User

Xargs is a filter that can pass parameters to a command, and a tool that combines multiple commands, dividing a stream into small enough chunks to facilitate the processing of filters and commands.


Typically, Xargs reads data from a pipe or stdin, but it can also read data from the output of a file.


The default command for Xargs is echo, which means that the input passed to Xargs by the pipeline will contain line breaks and whitespace, but with xargs processing, line breaks and whitespace will be replaced by spaces.


Xargs is a powerful command that captures the output of a command and then passes it to another command, and here are some practical examples of how to use xargs effectively.


EXAMPLES

Find/tmp-name Core-type F-print | Xargs/bin/rm-f

Find files named core in or below the directory/tmp and delete them. Note that this would work incorrectly if there is any filenames containing newlines or spaces.


Find/tmp-name Core-type f-print0 | xargs-0/bin/rm-f

Find files named core in or below the directory/tmp and delete them, processing filenames in such a-the-file or di Rectory names containing spaces or newlines are correctly handled.


Find/tmp-depth-name Core-type F-delete

Find files named core in or below the directory/tmp and delete them, but more efficiently than in the previous example (b Ecause we avoid the need to use fork (2) and EXEC (2) to launch RM and we don ' t need the extra xargs process).


Cut-d:-f1 </etc/passwd | Sort | Xargs Echo

Generates a compact listing of the users on the system.


Xargs sh-c ' emacs ' [email protected] ' </dev/tty ' Emacs

Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on Xargs ' standard in  Put. This example achieves the same effect as BSD ' s-o option, but in a more flexible and portable the.


Find/etc-name "*.conf" | Xargs ls–l

Find all files ending with *.conf in or below the directory/etc.


Cat Url-list.txt | Xargs Wget–c

If you had a file that contains a lot of the URLs what the want to download, you can download all links using Xargs


Find/-name *.jpg-type F-print | Xargs Tar-cvzf images.tar.gz

Find all of the JPG files, and compress it


LS *.sh | Xargs-n1-i CP {}/external-hard-drive/directory

Copy all the shell files to a external hard drive


Find. -name "*.log"-print0 | xargs-0 sed-i ' s/aaa/bbb/g '

Find all files ending with *.log and to find the documents inside the contents of all the AAA characters is replaced by B Bb


Find-name "*.log"-mtime +10-print0 |xargs-0 RM-RFV

Visually delete files that end with log 10 days ago, including files with spaces:


Find. -name ' *.txt '-type f-print0 |xargs-0 grep-n ' aaa '

Displays the line containing AAA in the *.txt file and the line number listed earlier, if there are multiple files, which are shown earlier.

[[email protected] tmp]# find. -name ' *.txt '-type f-print0 |xargs-0 grep-n ' aaa '

./2.txt:1:aadddaaaaaddddddddaaaaaaa

./1.txt:1:aaaaaaaddddddddaaaaaaa



The connotation of-print0 and Xargs 0 in Find

By default, find each output a file name, followed by a newline character (' \ n '), so we see the output of find is one line:

[email protected] tmp]# LL

Total 0

-rw-r--r--1 root root 0 Jul 13:28 1.log

-rw-r--r--1 root root 0 Jul 13:28 2.log

-rw-r--r--1 root root 0 Jul 13:28 3.log

Delete all the. log files, which can be used with Xargs:

Find-name ' *.log ' | Xargs RM


[Email protected] tmp]# ls-l

Total 0

-rw-r--r--1 root root 0 Jul 15:53 a 1.log

-rw-r--r--1 root root 0 Jul 15:53 a 2.log

-rw-r--r--1 root root 0 Jul 15:53 a 3.log


[Email protected] tmp]# find-name ' *.log ' | Xargs RM

Rm:cannot Remove '/a ': No such file or directory

Rm:cannot remove ' 1.log ': No such file or directory

Rm:cannot Remove '/a ': No such file or directory

Rm:cannot remove ' 2.log ': No such file or directory

Rm:cannot Remove '/a ': No such file or directory

Rm:cannot remove ' 3.log ': No such file or directory

The reason is very simple, xargs by default is a blank character (space, TAB, line break) to split the record, so the file name./A 1.log is interpreted as two records./A and 1.log, unfortunately RM cannot find these two files.

Ways to resolve this type of problem:

Find prints out a file name and then outputs a null character (' Xargs ') instead of a newline character, and then tells the to use the null character as the delimiter for the record.

This is the meaning of the-print0 and Xargs of Find-0.

In the general programming language, ' \ s ' is used as the end flag of a string, and the path name of the file may not contain the ' \ t ' character. It's probably the same with the other characters.


This article is from the "Bright Corner" blog, please be sure to keep this source http://larryzhao.blog.51cto.com/1030646/1533350

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.