Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to understand the syntax of the command find

Tags:

find

shell

The find command seems to differ from other Unix commands.

Why is there the empty curly brackets and a backward flash at the end of the following command?

find * -perm 777 -exec chmod 770 {} \;

I found one reason for the curly brackets but not for the backward flash.

The curly brackets are apparently for the path

Same as -exec, except that ``{}'' is replaced with as many pathnames as possible for each invocation of utility

like image 243
Léo Léopold Hertz 준영 Avatar asked Mar 22 '09 00:03

Léo Léopold Hertz 준영


2 Answers

The -exec command may be followed by any number of arguments that make up the command that is to be executed for each file found. There needs to be some way to identify the last argument. This is what \; does. Note that other things may follow after the -exec switch:

find euler/ -iname "*.c*" -exec echo {} \; -or -iname "*.py" -exec echo {} \;

(This finds all c-files and python files in the euler directory.)

The reason that exec does not require the full command to be inside quotes, is that this would require escaping a lot of quotes inside the command, in most circumstances.

like image 56
Stephan202 Avatar answered Oct 07 '22 20:10

Stephan202


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?

like image 36
kenorb Avatar answered Oct 07 '22 22:10

kenorb