The exec of the Linux find command

Source: Internet
Author: User

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 {} \;(the find 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-execls-l {} \;-rw-r--r--1Root root127 Ten- -  -:Wuyi./Log2014.log-rw-r--r--1Root root0 Ten- -  -: -./test4/log3-2. Log-rw-r--r--1Root root0 Ten- -  -: -./test4/log3-3. Log-rw-r--r--1Root root0 Ten- -  -: -./test4/log3-1. Log-rw-r--r--1Root root - Ten- -  -: Wu./Log2013.log-rw-r--r--1Root root302108  One-Geneva  .: +./Log2012.log-rw-r--r--1Root root - Ten- -  -: Geneva./Log.log-rw-r--r--1Root rootPanax Notoginseng Ten- -  -: -./Log.txt-rw-r--r--1Root root0 Ten- -  -: -./test3/log3-2. Log-rw-r--r--1Root root0 Ten- -  -: -./test3/log3-3. Log-rw-r--r--1Root root0 Ten- -  -: -./test3/log3-1. log

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

command:find. -type f-mtime +14-exec rm {} \; (Before the shell can delete files in any way, you should look at the appropriate files before you must 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. )

[[email protected] test]# ll Total328-rw-r--r--1Root root302108  One-Geneva  .: +Log2012.log-rw-r--r--1Root root - Ten- -  -: WuLog2013.log-rw-r--r--1Root root127 Ten- -  -:Wuyilog2014.loglrwxrwxrwx1Root root7 Ten- -  the: -Log_link.logLog.log-rw-r--r--1Root root - Ten- -  -: GenevaLog.log-rw-r--r--1Root rootPanax Notoginseng Ten- -  -: -LOG.TXTDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -test3drwxrwxrwx2Root root4096 Ten- -  -: -Test4[[email protected] test]#Find. -type F-mtime + --execRM {} \; [[email protected] test]# ll Total312-rw-r--r--1Root root302108  One-Geneva  .: +log2012.loglrwxrwxrwx1Root root7 Ten- -  the: -Log_link.logLOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096  One- A  +: +test3drwxrwxrwx2Root root4096  One- A  +: +Test4[[email protected] test]#

  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 {} \;(find command finds all filenames in the current directory ending with. Log, changing files over 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. )

[[email protected] test]# ll Total312-rw-r--r--1Root root302108  One-Geneva  .: +log2012.loglrwxrwxrwx1Root root7 Ten- -  the: -Log_link.logLOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096  One- A  +: +test3drwxrwxrwx2Root root4096  One- A  +: +Test4[[email protected] test]#Find. -name"*.log"-mtime +5-okRM {} \;<RM... . /log_link.log >?y<RM... . /log2012.log >?N[[email protected] test]# ll Total312-rw-r--r--1Root root302108  One-Geneva  .: +LOG2012.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096  One- A  +: +test3drwxrwxrwx2Root root4096  One- A  +: +Test4[[email protected] test]#

  Using the grep command in instance 4:-exec

command:find/etc-name "passwd*"-exec grep "root" {} \;(Any form of command can be used in the-EXEC option. 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. )

Find " passwd* " grep " Root " {} \;root:x: 0:0: root:/root:/bin/bashroot:x:0:0: root:/root:/bin/ Bash[[email protected] test]#

  Example 5: Find files to move to the specified directory

command:find. -name "*.log"-exec mv {}.. \;

[[email protected] test]# ll total 12drwxr-xr-x6Root root4096 Ten- -  on: -SCFDRWXRWXR-X2Root root4096  One- A  A: theTEST3DRWXRWXR-X2Root root4096  One- A  +: +Test4[[email protected] test]# CD test3/[[email protected] test3]# ll Total304-rw-r--r--1Root root302108  One-Geneva  .: +Log2012.log-rw-r--r--1Root root A  One- A  A: -Log2013.log-rw-r--r--1Root root0  One- A  A: -Log2014.log[[email protected] test3]#Find. -name"*.log"-execMV {} .. \; [[email protected] test3]# ll Total0[email protected] test3]# CD. [[email protected] test]# ll Total the-rw-r--r--1Root root302108  One-Geneva  .: +Log2012.log-rw-r--r--1Root root A  One- A  A: -Log2013.log-rw-r--r--1Root root0  One- A  A: -LOG2014.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -SCFDRWXRWXR-X2Root root4096  One- A  A: -TEST3DRWXRWXR-X2Root root4096  One- A  +: +Test4[[email protected] test]#

The exec of the Linux find command

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.