One Linux command per day exec of the--find command

Source: Internet
Author: User
Tags safe mode

Find is a very common Linux command, but we generally find out the amount is not only 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 ";" as the end of the sign, so the semicolon behind this command is indispensable, considering that the semicolon in each system has different meanings, so the front is preceded by a 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 LS or ls-l. Most users use some of the options to find old files and delete them. It is advisable to use the LS command to confirm that they are the files to be deleted before you actually execute the RM command to delete the files. 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.

This command is a bit of a pit, but it's really good to say it's a pit because I'm typing it. Get prompt: Find misses-exec parameters, ^^

Resolution: 1. Note is a pair of {}, a space and a \, and finally a semicolon

      2. in the \;    Use "\;"  ‘\;‘ So that they are brought up. "" caused by ", feel strange, but can not figure out how to describe"

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, and be careful when using such as MV or RM commands,

You can use the-EXEC option in Safe mode, and he will prompt you before each matching file is manipulated.

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 all files in the current directory that end with. Log, change the file that is above 5th, and delete them, and give a hint before deleting them. Press the Y key to confirm that the N key is canceled.

Example 4: Using the grep command in-exec

Command:

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

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", and then executes the grep command to see if there is a root user in those files.

Example 5: Find a file and move to the specified directory

Command:

Find. -name "*.log"-exec mv {}..    \;  .. is the path name

Example 6: Executing the CP command with the EXEC option

Command:

find.  -name "*.log"-exec cp {} test3 \;  accidentally again in the recruit, Test3 is a directory, otherwise CP does not go in.

  

One Linux command per day exec of the--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.