Xargs usage in Linux

Source: Internet
Author: User
When xargs uses the-exec option of the find command to process matched files, the find command passes all matched files to Exec for execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error will occur after the find command runs for several minutes. The error message is usually "the parameter column is too long" or "parameter column overflow ". This is the use of the xargs command, especially used with the find command. The find command passes the matching file to the xargs command, while the xargs command only obtains part of the file, not all of them, unlike the find-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this. Let's take a look at how the xargs command is used with the find command and give some examples. Search for the ". SVN" folder in the current directory and generate these folders. Run the "find" command to find the content, and then send it to the "RM" command to find. -type D-name ". SVN "| xargs Rm-RF; 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 filter and command processing. therefore, this command is a powerful replacement for post reference. when commands with too many parameters fail to be replaced, replacement with xargs is generally successful. in general, xargs reads data from a pipe or stdin, but it can also read data from the output of the file. the default xargs command is Echo. the following example deletes a large number of files ls | xargs-N 10 Rm-frls is of course the output of all file names (separated by spaces) xargs is to output ls, each 10 objects is a group (separated by spaces) and serves as the RM-RF parameter. In other words, 10 objects with all file names are deleted by RM-RF, this will not exceed the length of the command line Degree. The instance inserts the file name in the middle of the command line and enters: ls | xargs-t-I {} MV {}{}. old this command sequence is added at the end of each name. old to rename all files in the current directory. -The I flag tells the xargs command to insert each line of the LS directory list with {} (curly brackets. If the current directory contains files chap1, chap2, and chap3, the following command is constructed: MV chap1 chap1.oldmv chap2 chap2.oldmv chap3 chap3.old

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.