Find-exec in linux

Source: Internet
Author: User
The find-exec command for linux has recently been used to delete some folders in a directory. It is actually a folder named CVS. anyone who has used CVS knows that CVS will create a folder named CVS at each level of the Directory, containing information about CVS, I need... the find-exec command for linux has recently been used to delete some folders in a directory. It is actually a folder named "CVS". anyone who has used CVS knows that CVS creates a folder named CVS at each level of the Directory, containing information about CVS, I need to delete all the folders named "CVS" in a directory. In LINUX, it is actually very simple. use the find command: [plain] find. -name CVS-exec rm-rf {}\; it seems no problem, but an error is returned: [plain] find :'. /CVS ': No such file or directory. the/CVS folder exists and has been deleted. this is a perfect success, but why do you still report an error? There is another way to write [plain] find. -name CVS-exec rm-rf {} \ + is holding a try attitude. it is surprising that this method is successfully executed and no error is reported. this is confusing, no way to turn to man-exec command; Execute command; true if 0 status is returned. all following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. the string '{}' is replaced by the current file name being processed everywhere it occurs I N the arguments to the command, not just in arguments where it is alone, as in some versions of find. both of these constructions might need to be escaped (with a '\') or quoted to protect them from expansion by the shell. see the EXAMPLES sec-tion for examples of the use of the '-exec' option. the speci-fied command is run once for each matched file. the command is executed in the starting dire Ctory. there are unavoidable security problems surrounding use of the-exec option; you shocould use the-execdir option instead. another type:-exec command {} + This variant of the-exec option runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invoca-tions of the command will be much less than the number Matched files. the command line is built in much the same way that xargs builds its command lines. only one instance of '{}' is allowed within the command. the command is executed in the starting directory. after reading this for half a day, I noticed their difference, marked in red. That is to say, "-exec" + ";" executes a command for each matching file. Specifically, the rm-rf command is used here, "-exec" + "+" only append the matched file name as the command parameter to the end of the command, which is like this: rm-rf file1 file2 file3 why is this difference so obvious? After thinking about it, I realized that the execution process of #1 should be like this: 1. record and traverse all the directories and files on the surface layer, and compare whether they match. 2. matched the folder named CVS, and then executed the command rm-rf. /CVS, 3. recursively traverse all directories based on the previous records. when the program attempts to traverse the directory named "CVS" at this layer, it finds that the directory does not exist and reports an error. Why is this directory gone? ha, it has been deleted in step 2! Is that true? it can be verified: [plain] find. -maxdepth 1-name CVS-exec rm-rf {}\;-the maxdepth parameter specifies that the match only occurs in the current directory and does not go deep into the subdirectory. The result is that no error is reported for this command, it also verifies the previous conjecture. Record here for future query.
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.