简体   繁体   中英

What's the meaning of '{}' in a find command?

我想知道{}实际作用及其在此命令中的工作方式:

find .-type f ! -name '*.c' -exec rm {} +

From man find :

-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 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 to protect them from expansion by the shell.

and

-exec command {} +

This variant of the -exec action 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 invocations of the command will be much less than the number of 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.

So what it does is to use the results of the find command as argument on the rm execution.

Then, the full command

find .-type f ! -name '*.c' -exec rm {} +

will look for files in the current directory structure. It will match those not having a name ending with .c . For all the results, it will execute the rm command, so all not *.c files will be removed.

Example:

$ find . -type f
./two/three
./something
$ find . -type f -exec ls -ltr {} +
-rw-rw-r-- 1 me me 0 Sep 30 11:37 ./something
-rw-r--r-- 1 me me 0 Nov  1 17:50 ./two/three

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM