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

Source: Internet
Author: User
Tags touch command
Every day, a linux command (21): link to the find command xargs: A linux command (1): ls command (2): cd command http://www.2cto.com/ OS /201210/163050.html#every day ..
Link to xargs of one linux command (21): find command every day: one linux command every day (1): ls command http://www.2cto.com/os/201210/163049.html ; One linux command every day (2): cd command http://www.2cto.com/os/201210/163050.html ; One linux command every day (3): pwd command http://www.2cto.com/os/201210/163462.html ; One linux command every day (4): mkdir command http://www.2cto.com/os/201210/163463.html ; One linux command every day (5): rm command http://www.2cto.com/os/201210/163662.html ; One linux command (6) every day: rmdir command http://www.2cto.com/os/201210/164017.html ; One linux command (7) every day: mv command http://www.2cto.com/os/201210/164247.html ; One linux command every day (8): cp command http://www.2cto.com/os/201210/164254.html ; One linux command every day (9): touch Command http://www.2cto.com/os/201211/165699.html ; One linux command every day (10): cat command http://www.2cto.com/os/201211/165989.html ; One linux command every day (11): nl command http://www.2cto.com/os/201211/165990.html One linux command every day (12): more command http://www.2cto.com/os/201211/165994.html One linux command every day (13): less command http://www.2cto.com/os/201211/165998.html One linux command every day (14): head Command http://www.2cto.com/os/201211/166191.html One linux command every day (15): tail command http://www.2cto.com/os/201211/168702.html One linux command every day (16): which command http://www.2cto.com/os/201211/168890.html A linux command (17) every day: whereis command http://www.2cto.com/os/201211/168893.html One linux command (18) every day: locate command http://www.2cto.com/os/201211/168895.html One linux command every day (19): find command overview http://www.2cto.com/os/201211/168897.html One linux command every day (20): find command exec http://www.2cto.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.