One linux Command (21) every day: find command xargs

Source: Internet
Author: User
Tags touch command


One linux Command (21) every day: Link to the find command xargs: One linux Command (1) every day: ls command running (2): cd command http://www.bkjia.com/ OS /210210/163050.html#linuxcommand every day (3 ): pwd command Export (4): mkdir command http://www.bkjia.com/ OS /201210/1620.3.html#a linuxcommand every day (5): rm command Export (6): rmdir command http://www.bkjia.com/ OS /201210/ 164017. html; one linux Command every day (7): mv command running (8): cp command http://www.bkjia.com/ OS /201210/163164.html#a linuxcommand every day (9): touch command running (10): cat command running (11 ): nl command http://www.bkjia.com/ OS /201211/165990.htmla linuxcommand every day (12): more command http://www.bkjia.com/ OS /201211/165994.htmla Linux every day Command (13): less command running (14): head command running (15): tail command running (16): which command http://www.bkjia.com/ OS /201211/168890.htmla linuxcommand every day (17): whereis command running (18 ): locate command http://www.bkjia.com/ OS /201211/168895.htmla linuxcommand every day (19): fin D command overview seek (20): find command exechttp: // www.bkjia.com/ OS /201211/168901.html when you use the-exec option of the find command to process matched files, the find command passes all matching 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 www.2cto.com passes the matching file to the xargs command, and the xargs command only obtains part of the file, not all, unlike the-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this. In some systems, the-exec option is used to initiate a corresponding process for processing each matching file, rather than executing all the matching files as parameters once; in some cases, there may be too many processes and the system performance may degrade, so the efficiency is not high. The xargs command has only one process. In addition, when using the xargs command, whether to obtain all parameters at a time or obtain parameters in batches, and the number of parameters obtained each time is determined based on the options of the command and the corresponding adjustable parameters in the system kernel. Use instance: instance 1: find every common file in the system, and then use the xargs command to test which file commands they belong to: find. -type f-print | xargs file output: [root @ localhost test] # ll total 312-rw-r -- r -- 1 root 302108 11-03 19 log2012.log-rw-r -- 1 root 0 11-12 log2013.log-rw -r -- 1 root 0 11-12 log2014.logdrwxr-xr-x 6 root 4096 10-27 058 scfdrwxrwxrwx 2 root 4096 11-12 19:32 test3drwxrwxrwx 2 root 4096 11-12 :32 test 4 [root @ localhost test] # find. -type f-print | xargs file. /log2014.log: empty. /log2013.log: empty. /log2012.log: ASCII text [root @ localhost test] # instance 2: Find the memory information dump file (core dump) throughout the system, and save the result to/tmp/core. command in log file: find/-name "core"-print | xargs echo "">/tmp/core. log output: [root @ localhost test] # find/-name "core"-print | xargs echo "">/tmp/core. log [root @ localhost test] # cd/tmp [root @ localhost tmp] # l L total 16-rw-r -- r -- 1 root 1524 11-12 core. logdrwx ------ 2 root 4096 11-12 ssh-TzcZDx1766drwx ------ 2 root 4096 11-12 ssh-ykiRPk1815drwx ------ 2 root 4096 11-03 vmware-root instance 3: find all files in the current directory with read, write, and execution permissions, and revoke the corresponding write permission command: find. -perm-7-print | xargs chmod o-w output: [root @ localhost test] # ll total 312-rw-r -- r -- 1 root 302108 11-03 06:19 log2012.log-rw-r -- 1 root Root 0 11-12 log2013.log-rw-r -- 1 root 0 11-12 log2014.logdrwxr-xr-x 6 root 4096 10-27 0scfdrwxrwxrwx 2 root 4096 11-12 19:32 test3drwxrwxrwx 2 root 4096 11-12 19:32 test4 [root @ localhost test] # find. -perm-7-print | xargs chmod o-w [root @ localhost test] # ll total 312-rw-r -- r -- 1 root 302108 11-03 19 log2012.log-rw-r -- r -- 1 root 0 11-12 log20 13. log-rw-r -- 1 root 0 11-12 log2014.logdrwxr-xr-x 6 root 4096 10-27 058 scfdrwxrwxr-x 2 root 4096 11-12 test3drwxrwxr-x 2 root 4096 11-12 test4 [root @ localhost test] # description: after the command is executed, the permissions of the scf, test3, and test4 folders change. Example 4: Use the grep command to search for the hostname in all common files. Command: find. -type f-print | xargs grep "hostname" output: [root @ localhost test] # find. -type f-print | xargs grep "hostname ". /lo G2013.log: hostnamebaidu = baidu.com. /log2013.log: hostnamesina = sina.com. /log2013.log: hostnames = true [root @ localhost test] # instance 5: Use the grep command to search for hostnames in all common files in the current directory. Command: find. -name \ *-type f-print | xargs grep "hostnames" output: [root @ peida test] # find. -name \ *-type f-print | xargs grep "hostnames ". /log2013.log: hostnamesina = sina.com. /log2013.log: hostnames = true [root @ localhost test] # Note: In the preceding example, \ is used to cancel the fin Special Meaning of * in shell in d command. Instance 6: Use xargs to execute the mv command: find. -name "*. log "| xargs-I mv {} test4 output: [root @ localhost test] # ll Total Bytes -rw-r -- r -- 1 root 302108 11-03 19 log2012.log-rw-r -- 1 root 61 11-12 log2013.log-rw -r -- 1 root 0 11-12 log2014.logdrwxr-xr-x 6 root 4096 10-27 058 scfdrwxrwxr-x 2 root 4096 11-12 test3drwxrwxr-x 2 root root 4096 11-12 test4 [root @ localhost test] # Cd test4/[root @ localhost test4] # ll total 0 [root @ localhost test4] # cd .. [root @ localhost test] # find. -name "*. log "| xargs-I mv {} test4 [root @ localhost test] # ll total 12drwxr-xr-x 6 root 4096 10-27 058 scfdrwxrwxr-x 2 root 4096 11-13 05:50 test3drwxrwxr-x 2 root 4096 11-13 05:50 test4 [root @ localhost test] # cd test4/[root @ localhost test4] # ll total 304-rw-r -- r -- 1 root 302108 11-12 2 Log2012.log-rw-r -- 1 root 61 11-12 log2013.log-rw-r -- 1 root 0 11-12 log2014.log [root @ localhost test4] # instance 7: after xargs is executed, the system prompts xargs: argument line too long. Solution: Command: find. -type f-atime + 0-print0 | xargs-0-l1-t rm-f output: [root @ pd test4] # find. -type f-atime + 0-print0 | xargs-0-l1-t rm-frm-f [root @ pdtest4] # Note:-l1 is a single process; -t indicates that the command instance is printed before processing 8: Use-I parameter to replace the previous output with {} by default, and-I parameter can Specify other replacement characters, such as the [] command in the example: output: [root @ localhost test] # ll total 12drwxr-xr-x 6 root 4096 10-27 scfdrwxrwxr-x 2 root 4096 11-13 test3drwxrwxr-x 2 root 4096 11-13 test4 [root @ localhost test] # cd test4 [root @ localhost test4] # find. -name "file" | xargs-I [] cp [] .. [root @ localhost test4] # ll total 304-rw-r -- r -- 1 root 302108 11-12 log2012.log-rw-r -- 1 root 61 11-12 Log2013.log-rw-r -- 1 root 0 11-12 log2014.log [root @ localhost test4] # cd .. [root @ localhost test] # ll Total Bytes -rw-r -- r -- 1 root 302108 11-13 03 log2012.log-rw-r -- 1 root 61 11-13 03 log2013.log-rw -r -- 1 root 0 11-13 log2014.logdrwxr-xr-x 6 root 4096 10-27 058 scfdrwxrwxr-x 2 root 4096 11-13 05:50 test3drwxrwxr-x 2 root root 4096 11-13 05: 50 test4 [root @ localhost test] # Note: replace the previous output with {} by default in the-I parameter, and specify other replacement characters in the-I parameter, for example, the use command of the-p Parameter of [] instance 9: xargs In the example: find. -name "*. log "| xargs-p-I mv {}.. output: [root @ localhost test3] # ll total 0-rw-r -- r -- 1 root 0 11-13 log2015.log [root @ localhost test3] # cd .. [root @ localhost test] # ll Total Bytes -rw-r -- r -- 1 root 302108 11-13 03 log2012.log-rw-r -- 1 root 61 11-13 03 log2013.log-rw -r -- 1 root Root 0 11-13 log2014.logdrwxr-xr-x 6 root 4096 10-27 scfdrwxrwxr-x 2 root 4096 11-13 test3drwxrwxr-x 2 root 4096 11-13 test4 [root @ localhost test] # cd test3 [root @ localhost test3] # find. -name "*. log "| xargs-p-I mv {}.. mv. /log2015.log ..?... Y [root @ localhost test3] # ll total 0 [root @ localhost test3] # cd .. [root @ localhost test] # ll Total Bytes -rw-r -- r -- 1 root 302108 11-13 03 log2012.log-rw-r -- 1 root 61 11-13 03 log2013.log-rw -r -- 1 root 0 11-13 log2014.log-rw-r -- 1 root 0 11-13 06: 06 log2015.logdrwxr-xr-x 6 root 4096 10- 27 scfdrwxrwxr-x 2 root 4096 11-13 test3drwxrwxr-x 2 root 4 096 11-13 test4 [root @ localhost test] # Note: The-p Parameter prompts you to confirm whether to execute the following command. Run y, and run n.

Related Article

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.