Use of Linux xargs commands

Source: Internet
Author: User

The use of xargs commands in Linux 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. 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 you have a file containing a lot of 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 EXAMPLESfind/tmp-name core-type f-print | xargs/bin/rm-fFind files named core in or below the directory/tmp and delete them. note that this will work incorrectly if there are any filenames containing newlines or spaces. find/tmp-name core-type f-print0 | xargs-0/bin/rm-fFind files named core in or below the directory/tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled. find/tmp-depth-name core-type f-deleteFind files named core in or below the directory/tmp and delete them, but more efficiently than in the previous example (because 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 echoGenerates a compact listing of all the users on the system. xargs sh-c 'emacs "$ @" </dev/tty 'emacsLaunches the minimum number of copies of emacs needed, one after the other, to edit the files listed on xargs 'standard input. this example achieves the same effect as BSD's-o option, but in a more flexible and portable way.

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.