Unix find command, what are the {} and \; for?

后端 未结 5 957
慢半拍i
慢半拍i 2020-11-28 15:22

With this set of commands, what are the {} and \\; characters for?

find . -name \'*.clj\' -exec grep -r resources {} \\;


        
相关标签:
5条回答
  • 2020-11-28 15:33

    See man find. (particular the part about -exec)

    When using -exec to run a command on each of the files found, the {} is replaced with the name of each file found, and the command is terminated by \;

    In your example, all files found under the current directory (.), matching the name *.clj will have the command grep -r resources run on them (to find the string resources if it exists in each of those files).

    It's actually somewhat redundant, since -r is for recursively searching subdirectories, and that's what find is already doing.

    0 讨论(0)
  • 2020-11-28 15:33

    The character string "{}" will be replaced by the current file being processed. The escaped semi-colon terminates the command argument for the -exec option.

    0 讨论(0)
  • 2020-11-28 15:34

    Consider this alternative command which I find easier to understand:

    find . -name *.clj | xargs grep -r resources 
    
    0 讨论(0)
  • 2020-11-28 15:39

    The string {} in find is replaced by the pathname of the current file.

    The semicolon is used for terminating the shell command invoked by find utility.

    It needs to be escaped, or quoted, so it won't be interpreted by the shell, because ; is one of the special characters used by shell (list operators).

    See also: Why are the backslash and semicolon required with the find command's -exec option?

    0 讨论(0)
  • 2020-11-28 15:50

    In find, the -exec parameter grabs the rest of the parameters up til the ; (semicolon) which has to be escaped, hence the \;. Within this span, {} is replaced with the filename being inspected.

    0 讨论(0)
提交回复
热议问题