How to use the find command exec in linux

Source: Internet
Author: User
Find is a commonly used Linux command, but we generally find it not only for a look, but also for further operations. at this time, the role of exec will show the label: LinuxFind

Find is a commonly used Linux command, but we generally look for it not only, but also further operations. at this time, the role of exec becomes apparent.

Exec explanation:

-The exec parameter is followed by the command, and its termination is the end sign. 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:


Copy codeThe code is as follows:
[Root @ localhost test] # find.-type f-exec ls-l {}\;
-Rw-r -- 1 root 127 10-28 16:51./log2014.log
-Rw-r -- 1 root 0 10-28./test4/log3-2.log
-Rw-r -- 1 root 0 10-28./test4/log3-3.log
-Rw-r -- 1 root 0 10-28./test4/log3-1.log
-Rw-r -- 1 root 33 10-28 16:54./log2013.log
-Rw-r -- 1 root 302108 11-03 06:19./log2012.log
-Rw-r -- 1 root 25 10-28 :02./log. log
-Rw-r -- 1 root 37 10-28 :07./log.txt
-Rw-r -- 1 root 0 10-28./test3/log3-2.log
-Rw-r -- 1 root 0 10-28./test3/log3-3.log
-Rw-r -- 1 root 0 10-28./test3/log3-1.log
[Root @ localhost test] #

Note:

In the preceding example, the find command matches all common files in the current directory and lists them using the ls-l command in the-exec option.

Instance 2: find the files whose modification time is earlier than n days in the directory and delete them.

Command:

Find.-type f-mtime + 14-exec rm {}\;

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ll
Total 328
-Rw-r -- 1 root 302108 11-03 06:19 log2012.log
-Rw-r -- 1 root 33 10-28 16:54 log2013.log
-Rw-r -- 1 root 127 10-28 16:51 log2014.log
Lrwxrwxrwx 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 log.txt
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxrwx 2 root 4096 10-28 test3
Drwxrwxrwx 2 root 4096 10-28 test4
[Root @ localhost test] # find.-type f-mtime + 14-exec rm {}\;
[Root @ localhost test] # ll
Total 312
-Rw-r -- 1 root 302108 11-03 06:19 log2012.log
Lrwxrwxrwx 1 root 7 10-28 log_link.log-> log. log
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxrwx 2 root 4096 11-12 :32 test3
Drwxrwxrwx 2 root 4096 11-12 :32 test4
[Root @ localhost test] #

Note:

Before deleting a file in shell, check the corresponding file. 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. a prompt is displayed before deletion.

Command:

Find.-name "*. log"-mtime + 5-OK rm {}\;

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ll
Total 312
-Rw-r -- 1 root 302108 11-03 06:19 log2012.log
Lrwxrwxrwx 1 root 7 10-28 log_link.log-> log. log
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxrwx 2 root 4096 11-12 :32 test3
Drwxrwxrwx 2 root 4096 11-12 :32 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 -- 1 root 302108 11-03 06:19 log2012.log
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxrwx 2 root 4096 11-12 :32 test3
Drwxrwxrwx 2 root 4096 11-12 :32 test4
[Root @ localhost test] #

Note:

In the preceding example, the find command searches for all files whose names end with. log and whose names have been modified for more than five days in the current directory, and deletes the files. a prompt is displayed before deletion. Press y to delete the file, and press n to not delete the file.

Example 4: Use the grep command in-exec

Command:

Find/etc-name "passwd *"-exec grep "root "{}\;

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # find/etc-name "passwd *"-exec grep "root "{}\;
Root: x: 0: 0: root:/bin/bash
Root: x: 0: 0: root:/bin/bash
[Root @ localhost test] #

Note:

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: move the search file to the specified directory

Command:

Find.-name "*. log"-exec mv {}..\;

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ll
Total 12drwxr-xr-x 6 root 4096 10-27 scf
Drwxrwxr-x 2 root 4096 11-12 22:49 test3
Drwxrwxr-x 2 root 4096 11-12 :32 test4
[Root @ localhost test] # cd test3/
[Root @ localhost test3] # ll
Total 304
-Rw-r -- 1 root 302108 11-03 06:19 log2012.log
-Rw-r -- 1 root 61 11-12 log2013.log
-Rw-r -- 1 root 0 11-12 22:25 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 316
-Rw-r -- 1 root 302108 11-03 06:19 log2012.log
-Rw-r -- 1 root 61 11-12 log2013.log
-Rw-r -- 1 root 0 11-12 22:25 log2014.log
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxr-x 2 root 4096 11-12 22:50 test3
Drwxrwxr-x 2 root 4096 11-12 :32 test4
[Root @ localhost test] #

Instance 6: run the cp command with the exec option

Command:

Find.-name "*. log"-exec cp {} test3 \;

Output:


Copy codeThe code is as follows:
[Root @ localhost test3] # ll
Total 0 [root @ localhost test3] # cd ..
[Root @ localhost test] # ll
Total 316
-Rw-r -- 1 root 302108 11-03 06:19 log2012.log
-Rw-r -- 1 root 61 11-12 log2013.log
-Rw-r -- 1 root 0 11-12 22:25 log2014.log
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxr-x 2 root 4096 11-12 22:50 test3
Drwxrwxr-x 2 root 4096 11-12 :32 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 -- 1 root 302108 11-12 22:54 log2012.log
-Rw-r -- 1 root 61 11-12 22:54 log2013.log
-Rw-r -- 1 root 0 11-12 22:54 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.