Linux Command xargs + cut

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. 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

 

Instance: connect all files in a directory to a new directory.

CD $ newdir

Ls $ olddir | xargs-t-I '{} 'ln-S $ olddir /'{}''{}'

If you only need to create files with certain conditions, add | grep XXX in the middle.

 

2. Select a fixed segment of text in each line of the file. Cut-c1-72 file: The result is 1st to 72 columns per line of file. Cut-F1, file: when multiple fields are separated by commas (,) or spaces, the result is the and 5 fields of each line of file. Cut-D: indicates that the field separator in each line is :. For example, cut-D:-F1, 5/etc/passwd, get the ing of all user IDs and names.

 

 

Complete!

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.