Introduction to exec usage of find command in Linux system

Source: Internet
Author: User
Tags safe mode

Find is one of our most common Linux commands, but we generally look for more than just look, there will be further operations, this time the role of exec is revealed.

  EXEC explained:

The-exec parameter is followed by the command command, which terminates with; For the end sign, so the semicolon behind the command is indispensable, and the preceding backslash is preceded by a semicolon, given the different meanings of semicolons in each system.

  {} curly braces represent the names of the files found in the previous find.

Use Find, as long as you want to write the operation in a file, you can use the exec to match found search, 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 old files and delete them. It is recommended that you take a look at the LS command before you actually perform the RM command to delete files, and confirm that they are the files you want to delete. The EXEC option follows the command or script to be executed, followed by a pair of {}, a space and one, and finally a semicolon. In order to use the EXEC option, you must also use the Print option. If you verify the Find command, you will see that the command outputs only the relative path and file name from the current path.

  The instance 1:ls-l command is placed in the-exec option of the Find command

Command:

Find. -type f-exec ls-l {};

Output:

Copy Code

The code is as follows:

[Root@localhost test]# Find. -type f-exec ls-l {};

-rw-r--r--1 root root 127 10-28 16:51./log2014.log

-rw-r--r--1 root 0 10-28 14:47./test4/log3-2.log

-rw-r--r--1 root 0 10-28 14:47./test4/log3-3.log

-rw-r--r--1 root 0 10-28 14:47./test4/log3-1.log

-rw-r--r--1 root 10-28 16:54./log2013.log

-rw-r--r--1 root root 302108 11-03 06:19./log2012.log

-rw-r--r--1 root 10-28 17:02./log.log

-rw-r--r--1 root root radix 10-28 17:07./log.txt

-rw-r--r--1 root 0 10-28 14:47./test3/log3-2.log

-rw-r--r--1 root 0 10-28 14:47./test3/log3-3.log

-rw-r--r--1 root 0 10-28 14:47./test3/log3-1.log

[Root@localhost test]#

Description

In the example above, the Find command matches all the normal files in the current directory and lists them using the Ls-l command in the-exec option.

  Example 2: Find files in the directory that have changed time before n days and delete them

Command:

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

Output:

Copy Code

The code is as follows:

[Root@localhost test]# LL

Total 328

-rw-r--r--1 root root 302108 11-03 06:19 log2012.log

-rw-r--r--1 root 10-28 16:54 log2013.log

-rw-r--r--1 root root 127 10-28 16:51 Log2014.log

lrwxrwxrwx 1 root 7 10-28 15:18 log_link.log-> log.log

-rw-r--r--1 root 10-28 17:02 log.log

-rw-r--r--1 root radix 10-28 17:07 log.txt

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 10-28 14:47 test3

DRWXRWXRWX 2 root root 4096 10-28 14:47 test4

[Root@localhost test]# Find. -type f-mtime +14-exec rm {};

[Root@localhost test]# LL

Total 312

-rw-r--r--1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root 7 10-28 15:18 log_link.log-> log.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 11-12 19:32 test3

DRWXRWXRWX 2 root root 4096 11-12 19:32 test4

[Root@localhost test]#

Description

Before the shell deletes files in any way, you should look at the appropriate files, and be careful! You can use the-EXEC option's Safe Mode when you use a command such as MV or RM. It will prompt you before the operation of each matching file.

  Example 3: Find files in the directory that have changed time before n days and delete them before deleting them before removing them

Command:

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

Output:

Copy Code

The code is as follows:

[Root@localhost test]# LL

Total 312

-rw-r--r--1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root 7 10-28 15:18 log_link.log-> log.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 11-12 19:32 test3

DRWXRWXRWX 2 root root 4096 11-12 19: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--r--1 root root 302108 11-03 06:19 log2012.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 11-12 19:32 test3

DRWXRWXRWX 2 root root 4096 11-12 19:32 test4

[Root@localhost test]#

Description

In the example above, the Find command finds all file names in the current directory with the end of the. Log, changes the time above 5th, and deletes them, except to give a hint before deleting them. Press Y to delete the file, press N to not delete.

  Use grep command in instance 4:-exec

Command:

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

Output:

Copy Code

The code is as follows:

[Root@localhost test]# find/etc-name "passwd*"-exec grep "root" {};

Root:x:0:0:root:/root:/bin/bash

Root:x:0:0:root:/root:/bin/bash

[Root@localhost test]#

Description

Any form of command can be used in the-EXEC option. In the example above we use the grep command. The find command first matches all files named "passwd*", such as passwd, Passwd.old, Passwd.bak, and then executes the grep command to see if there is a root user in those files.

  Instance 5: Find file move to specified directory

Command:

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

Output:

Copy Code

The code is as follows:

[Root@localhost test]# LL

Total 12drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

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

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

[Root@localhost test]# CD test3/

[Root@localhost test3]# LL

Total 304

-rw-r--r--1 root root 302108 11-03 06:19 log2012.log

-rw-r--r--1 root 11-12 22:44 log2013.log

-rw-r--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--r--1 root root 302108 11-03 06:19 log2012.log

-rw-r--r--1 root 11-12 22:44 log2013.log

-rw-r--r--1 root 0 11-12 22:25 log2014.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

Drwxrwxr-x 2 root root 4096 11-12 22:50 test3

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

[Root@localhost test]#

  Example 6: Execute the CP command with the EXEC option

Command:

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

Output:

Copy Code

The code is as follows:

[Root@localhost test3]# LL

Total 0[root@localhost test3]# CD ...

[Root@localhost test]# LL

Total 316

-rw-r--r--1 root root 302108 11-03 06:19 log2012.log

-rw-r--r--1 root 11-12 22:44 log2013.log

-rw-r--r--1 root 0 11-12 22:25 log2014.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

Drwxrwxr-x 2 root root 4096 11-12 22:50 test3

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

[Root@localhost test]# Find. -name "*.log"-exec cp {} test3;

CP: "./test3/log2014.log" and "Test3/log2014.log" as the same file

CP: "./test3/log2013.log" and "Test3/log2013.log" as the same file

CP: "./test3/log2012.log" and "Test3/log2012.log" as the same file

[Root@localhost test]# CD Test3

[Root@localhost test3]# LL

Total 304

-rw-r--r--1 root root 302108 11-12 22:54 log2012.log

-rw-r--r--1 root 11-12 22:54 log2013.log

-rw-r--r--1 root 0 11-12 22:54 log2014.log

[Root@localhost test3]#

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.