Simple EXEC command usage and find usage examples

Source: Internet
Author: User

Find is a commonly used Linux Command, but we generally look for it not only, but also further operations. At this time, the role of exec becomes apparent.

Example:

find ./ -name "*.txt" -exec ls -l "{}" \;

Find the TXT file of the current directory and list it in the form of a list

find ./ -name "*.txt" -exec mv "{}" "{}d" \;

Modify the batch quantity to the suffix of the. txt file under the current directory and change it to. txt D.

Explanation:

-The exec parameter is followed by the command, and its termination is the end sign. Therefore, the semicolon after this command is indispensable, given that semicolons in various systems have different meanings, the front is followed by a backslash.

{} Curly brackets indicate the names of the files found by the previous find.

Example:

find ./ -name "*.tmp" -exec rm -rf "{}" \;

Delete all temporary files

-Exec syntax format:

-Exec command {}\; // note that spaces must exist {}\;

Shell's built-in command exec will not start a new shell, but will replace the current shell process with the command to be executed and clean up the environment of the old process, in addition, other commands after the exec command will not be executed.
Therefore, if you execute exec ls in a shell, the shell will exit after the current directory is listed, because the shell process has been replaced by a process that only executes the LS command, the execution will naturally exit after the execution ends. To avoid this impact, we usually place the exec command in a shell script and use the main script to call this script. Bash A can be used to call the point. sh, (. SH is the script used to store the command. sh creates a sub shell for execution. After exec is executed, the sub-script process is replaced with the corresponding EXEC command.
The source command or "." does not create a shell for the script, but only executes the commands contained in the script in the current shell.
However, pay attention to one exception. When the exec command is used to operate the file descriptor, the shell will not be replaced, and after the operation is complete, the following command will continue to be executed.
Exec 3 <& 0: This command points Operator 3 to the standard input.
Elsewhere, this command can also be used as an option for the find command, as shown below:
(1) In the current directory (including sub-Directories), find all TXT files and find the rows containing the string "bin"

find ./ -name "*.txt" -exec grep "bin" {} \;


(2) Delete All TXT files in the current directory (including subdirectories)

find ./ -name "*.log" -exec rm {} \;



This article is from the "Mr. sor" blog, please be sure to keep this source http://professor.blog.51cto.com/996189/1571873

Simple EXEC command usage and find usage examples

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.