Find command Exec

Source: Internet
Author: User
Find command Exec

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:

[[Email protected] 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

[[Email protected] 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:

[[Email protected] 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 Root 4096 10-28 test3

Drwxrwxrwx 2 root Root 4096 10-28 test4

[[Email protected] Test] # Find.-Type F-mtime + 14-exec RM {}\;

[[Email protected] 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 Root 4096 11-12 :32 test3

Drwxrwxrwx 2 root Root 4096 11-12 :32 test4

[[Email protected] 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:

[[Email protected] 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 Root 4096 11-12 :32 test3

Drwxrwxrwx 2 root Root 4096 11-12 :32 test4

[[Email protected] Test] # Find.-Name "*. log"-mtime + 5-OK RM {}\;

<RM.../log_link.log>? Y

<RM.../log2012.log>? N

[[Email protected] 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 Root 4096 11-12 19:32 test3

Drwxrwxrwx 2 root Root 4096 11-12 :32 test4

[[Email protected] 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:

[[Email protected] Test] # Find/etc-name "passwd *"-exec grep "root "{}\;

Root: X: 0: 0: Root:/root:/bin/bash

Root: X: 0: 0: Root:/root:/bin/bash

[[Email protected] 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:

[[Email protected] Test] # ll

Total 12drwxr-xr-x 6 Root 4096 10-27 SCF

Drwxrwxr-x 2 root Root 4096 11-12 22:49 test3

Drwxrwxr-x 2 root Root 4096 11-12 19:32 test4

[[Email protected] Test] # cd test3/

[[Email protected] 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

[[Email protected] test3] # Find.-Name "*. log"-exec MV {}..\;

[[Email protected] test3] # ll

Total 0 [[email protected] test3] # CD ..

[[Email protected] 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 Root 4096 11-12 22:50 test3

Drwxrwxr-x 2 root Root 4096 11-12 :32 test4

[[Email protected] Test] #

Instance 6: run the CP command with the exec Option

Command:

Find.-Name "*. log"-exec CP {} test3 \;

Output:

[[Email protected] test3] # ll

Total 0 [[email protected] test3] # CD ..

[[Email protected] 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 Root 4096 11-12 22:50 test3

Drwxrwxr-x 2 root Root 4096 11-12 :32 test4

[[Email protected] 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

[[Email protected] Test] # cd test3

[[Email protected] 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

[[Email protected] test3] #

Find command Exec

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.