Recently, you need 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:
Find.-Name CVS-exec Rm-RF {}\;
It seems that there is no problem, but an error is reported:
Find: './CVS': no such file or directory
After checking, I found that the./CVS folder exists and has been deleted. This is a complete success, but why do I still report an error?
You can't remember another way to write the following code:
Find.-Name CVS-exec Rm-RF {} \ +
Unexpectedly, this method was successfully executed and no error was reported. This is confusing and there is 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
An argument consisting of ';' is encountered. The string '{}'
Is replaced by the current file name being processed everywhere
It occurs in 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
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 directory. 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, 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 '{}'
Is allowed within the command. The command is executed in
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, that is, Rm-RF file1 file2 file3
But why is this difference so obvious? I thought about it and realized:
# The Execution Process of 1 should be as follows:
1. Record and traverse all directories and files on the surface layer, and compare whether the files match.
2. matched the folder named CVs and executed the command Rm-RF./CVS,
3. recursively traverse all directories based on previous records. The problem occurs whenProgramIf you try to traverse the directory named "CVS" at this layer, you will find that this directory does not exist, so an error is returned. Why is this directory gone? Ha, it has been deleted in step 2!
Is that true? It can be verified:
Find.-maxdepth 1-name CVS-exec Rm-RF {}\;
-The maxdepth parameter indicates that the match only occurs in the current directory and does not go deep into the subdirectory. The result is that this command does not report an error and verifies the previous conjecture.
Record here for future query.