One linux Command every day (20): find command exec

Source: Internet
Author: User


Every day, a linux Command (20): find command exec link: Every day a linux Command (1): ls command running (2): cd command http://www.bkjia.com/ OS /201210/163050.html#a linuxcommand (3) every day ): 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/1 64017. 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.htmllinuxevery day Order (13): less command limit (14): head command limit (15): tail command limit (16): which command http://www.bkjia.com/ OS /201211/168890.htmla linuxcommand every day (17): whereis command limit (18 ): locate command http://www.bkjia.com/ OS /201211/168895.htmla linuxcommand every day (19): find Command overview http://www.bkjia.com/ OS /201211/168897.html find is a very common Linux Command, but we generally find out not only look at it, there will be further operations, the role of exec will appear at this time. Www.2cto.com exec: The-exec parameter is followed by the command, and its termination is ";". Therefore, the semicolon after this command is indispensable, given that semicolons in various systems have different meanings, the front is followed by a backslash. {} Curly brackets indicate the names of the files found by the previous find. When using find, you only need to write the desired operation in a file, and you can use exec to search with find, which is very convenient. In some operating systems, only the-exec option is allowed to execute commands such as l s or ls-l. Most users use this option to find and delete old files. Before executing the rm command to delete files, we recommend that you use the ls command to check whether they are the files to be deleted. The exec option is followed by the command or script to be executed, followed by a pair of {}, a space, a \, and a semicolon. To use the exec option, you must use the print option at the same time. If you verify the find command, you will find that this command only outputs the relative path and file name from the current path. Instance 1: place the ls-l command in the-exec option of the find command. Command: find. -type f-exec ls-l {}\; output: [root @ localhost test] # find. -type f-exec ls-l {}\;-rw-r -- 1 root 127 10-28. /log2014.log-rw-r -- 1 root 0 10-28 14:47. /test4/log3-2.log-rw-r -- r -- 1 root 0 10-28. /test4/log3-3.log-rw-r -- r -- 1 root 0 10-28. /test4/log3-1.log-rw-r -- r -- 1 root 33 10-28. /log2013.log-rw-r -- r- -1 root 302108 11-03. /log2012.log-rw-r -- 1 root 25 10-28 17:02. /log. log-rw-r -- 1 root 37 10-28 :07. /log.txt-rw-r -- 1 root 0 10-28 14:47. /test3/log3-2.log-rw-r -- r -- 1 root 0 10-28. /test3/log3-3.log-rw-r -- r -- 1 root 0 10-28. /test3/log3-1.log [root @ localhost test] # Note: In the above example, the find command matches all the common files in the current directory, and use the ls-l command in the-exec option to list them. Instance 2: find the files whose modification time is earlier than n in the directory and delete them. Command: find. -type f-mtime + 14-exec rm {}\; output: [root @ localhost test] # ll total 328-rw-r -- r -- 1 root 302108 11-03 19 log2012.log-rw-r -- 1 root 33 10-28 log2013.log-rw -r -- 1 root 127 10-28 log2014.loglrwxrwxrwx 1 root 7 10-28 log_link.log-> log. log-rw-r -- 1 root 25 10-28 17:02 log. log-rw-r -- 1 root 37 10-28 :07 l Og.txt drwxr-xr-x 6 root 4096 10-27 scfdrwxrwxrwx 2 root 4096 10-28 test3drwxrwxrwx 2 root 4096 10-28 test4 [root @ localhost test] # find. -type f-mtime + 14-exec rm {}\; [root @ localhost test] # ll total 312-rw-r -- r -- 1 root 302108 11-03 19 log2012.loglrwxrwxrwx 1 root 7 10-28 log_link.log-> log. logdrwxr-xr-x 6 root 4096 10-27 scfdrwxrwxrwx 2 root 4096 11-12 19:32 test3drwxrwxrwx 2 root 4096 11-12 19:32 test4 [root @ localhost test] # Note: before deleting a file in shell, check the corresponding file first. Be careful! When using commands such as mv or rm, you can use the-exec option safe mode. It will prompt you before performing operations on each matching file. Instance 3: find and delete files whose modification time is earlier than n in the directory. Before deletion, run the command find. -name "*. log "-mtime + 5-OK rm {}\; output: [root @ localhost test] # ll total 312-rw-r -- r -- 1 root 302108 11-03 19 log2012.loglrwxrwxrwx 1 root 7 10-28 log_link.log-> log. logdrwxr-xr-x 6 root 4096 10-27 scfdrwxrwxrwx 2 root 4096 11-12 test3drwxrwxrwx 2 root 4096 11-12 test4 [root @ localhost test] # find. -name "*. Log"-mtime + 5-OK rm {}\; <rm.../log_link.log>? Y <rm.../log2012.log>? N [root @ localhost test] # ll total 312-rw-r -- r -- 1 root 302108 11-03 19 log2012.logdrwxr-xr-x 6 root 4096 10-27 058 scfdrwxrwxrwx 2 root 4096 11-12 19:32 test3drwxrwxrwx 2 root 4096 11-12 19:32 test4 [root @ localhost test] # description: in the preceding example, the find command searches for all file names in the current directory. files whose names end with logs and whose change time is more than five days ago are deleted, but a prompt is provided before the deletion. Press y to delete the file, and press n to not delete the file. Instance 4:-exec run the grep command: find/etc-name "passwd *"-exec grep "root" {}\; output: [root @ localhost test] # find/etc-name "passwd *"-exec grep "root" {}\; root: x: 0: 0: root:/root: /bin/bashroot: x: 0: 0: root:/bin/bash [root @ localhost test] # description: any form of command can be used in the-exec option. In the above example, we use the grep command. The find command First matches all files named "passwd *", such as passwd, passwd. old, passwd. bak, and then runs the grep command to check whether a root user exists in these files. Instance 5: find the file and move it to the specified directory. Command: find. -name "*. log "-exec mv {}.. \; output: [root @ localhost test] # ll total 12drwxr-xr-x 6 root 4096 10-27 scfdrwxrwxr-x 2 root 4096 11-12 test3drwxrwxr-x 2 root 4096 11-12 test4 [root @ localhost test] # cd test3/[root @ localhost test3] # ll total 304-rw-r -- r -- 1 root 302108 11-03 06:19 log2012.log-rw-r -- 1 root root 61 11-12 log2013.log-rw-r -- 1 root 0 11-12 log2014.log [root @ localhost test3] # find. -name "*. log "-exec mv {}.. \; [root @ localhost test3] # ll total 0 [root @ localhost test3] # cd .. [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] # instance 6: run the cp command: find. -name "*. log "-exec cp {} test3 \; output: [root @ localhost test3] # ll total 0 [root @ localhost test3] # cd .. [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] # find. -name "*. log "-exec cp {} test3 \; cp:". /test3/log2014.log "and" test3/log2014.log "are the same file cp:". /test3/log2013.log "and" test3/log2013.log "are the same file cp:". /test3/log2012.log "and" test3/log2012.log "are the same file [root @ localhost test] # cd test3 [root @ localhost test3] # 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 test3] #
 

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.