Find is a very common Linux command, but what we generally find out is not just to look at it, there will be further operations, this time the role of exec is revealed.
EXEC explained:
The-exec parameter is followed by command, which terminates with a; for the end of the flag, so the semicolon behind this command is indispensable, considering that the semicolons in each system have different meanings, so precede the backslash.
{} curly braces represent the file name found in the previous find.
When using find, just write the desired action in a file, you can use the exec to match find find, 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 execute the RM command to delete files, confirming that they are the files you want to delete. The EXEC option is followed by the command or script that you want to execute, followed by a pair of {}, a space and a \, and finally a semicolon. In order to use the EXEC option, you must use the Print option at the same time. 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:
[[email protected] 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 root 0 10-28 14:47./test4/log3-2.log
-rw-r--r--1 root root 0 10-28 14:47./test4/log3-3.log
-rw-r--r--1 root root 0 10-28 14:47./test4/log3-1.log
-rw-r--r--1 root 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 root 10-28 17:02./log.log
-rw-r--r--1 root root Notoginseng 10-28 17:07./log.txt
-rw-r--r--1 root root 0 10-28 14:47./test3/log3-2.log
-rw-r--r--1 root root 0 10-28 14:47./test3/log3-3.log
-rw-r--r--1 root root 0 10-28 14:47./test3/log3-1.log
[Email protected] 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 change time before n days and delete them
Command:
Find. -type f-mtime +14-exec rm {} \;
Output:
[email protected] test]# LL
Total 328
-rw-r--r--1 root root 302108 11-03 06:19 log2012.log
-rw-r--r--1 root root 10-28 16:54 Log2013.log
-rw-r--r--1 root root 127 10-28 16:51 Log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log log.log
-rw-r--r--1 root root 10-28 17:02 log.log
-rw-r--r--1 root root notoginseng 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
[[email protected] test]# find. -type f-mtime +14-exec rm {} \;
[email protected] test]# LL
Total 312
-rw-r--r--1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root 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
[Email protected] test]#
Description
Before the shell can delete files in any way, you should check the corresponding files, be careful! You can use the Safe mode of the-EXEC option when using a command such as MV or RM. It will prompt you before you work on each file that is matched to it.
Example 3: Find files in the directory that change time before n days and delete them, give a hint before deleting
Command:
Find. -name "*.log"-mtime +5-ok rm {} \;
Output:
[email protected] test]# LL
Total 312
-rw-r--r--1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root 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
[[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--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
[Email protected] test]#
Description
In the example above, the Find command looks in the current directory for all file names ending with. Log, changing files that are older than 5th, and deleting them, except to give a hint before deleting them. Press the Y key to delete the file and press N to not delete it.
Using the grep command in instance 4:-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]#
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 these files.
Example 5: Find files to move to the specified directory
Command:
Find. -name "*.log"-exec mv {}.. \;
Output:
[email protected] 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
[Email protected] test]# CD test3/
[email protected] test3]# LL
Total 304
-rw-r--r--1 root root 302108 11-03 06:19 log2012.log
-rw-r--r--1 root root 11-12 22:44 Log2013.log
-rw-r--r--1 root 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--r--1 root root 302108 11-03 06:19 log2012.log
-rw-r--r--1 root root 11-12 22:44 Log2013.log
-rw-r--r--1 root 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
[Email protected] test]#
Example 6: Executing 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--r--1 root root 302108 11-03 06:19 log2012.log
-rw-r--r--1 root root 11-12 22:44 Log2013.log
-rw-r--r--1 root 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
[[email protected] test]# find. -name "*.log"-exec cp {} test3 \;
CP: "./test3/log2014.log" and "Test3/log2014.log" for the same file
CP: "./test3/log2013.log" and "Test3/log2013.log" for the same file
CP: "./test3/log2012.log" and "Test3/log2012.log" for the same file
[Email protected] test]# CD TEST3
[email protected] test3]# LL
Total 304
-rw-r--r--1 root root 302108 11-12 22:54 log2012.log
-rw-r--r--1 root root 11-12 22:54 Log2013.log
-rw-r--r--1 root root 0 11-12 22:54 log2014.log
[Email protected] test3]#
One Linux command per day: exec of the Find command