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.
e xec Explanation:
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 the Find lookup, very convenient . in some operating systems, only the-EXEC option is allowed to execute commands such as LS 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 .
Example 1 :The ls-l command is placed in the-exec option of the Find command
command: find.-type f-exec ls-l {} \;
Description: Thefind command matches all normal files in the current directory and lists them using the Ls-l command in the-exec option.
[[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 ro OT 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 33 1 0-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 Roo T 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]#
Example 2: find changes in the directory in N files and delete them before the day
command: find.-type f-mtime +14-exec rm {} \;
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.
< Span style= "Font-family:comic Sans MS; font-size:14px "> instance 3: Find the change time in the directory n previous files and delete them , prompt before deleting
< Span style= "Font-family:comic Sans MS; font-size:14px "> command: find . -name "*.log" -mtime +5 -ok rm {} \;
Description : in the on example, the Find command finds all filenames in the current directory. Log end, change the files over the 5th, and delete them, but give a hint before deleting them. Press the Y key to delete the file and press N to not delete it.
Example 4: -exec in using the grep command
command: find/etc-name "passwd*"-exec grep "root" {} \;
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 "spp" keyword in these files .
Example 5: find files and move to the specified directory
command: find.-name "*.log"-exec mv {}...
Example 6: execute the CP command with the EXEC option
command: find.-name "*.log"-exec cp {} test3 \;
Linux common commands (19)-Exec of Find