tags:

views:

119

answers:

3

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

+6  A: 

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.

Stephan202
+2  A: 

The (escaped) semicolon is needed so that "find" can tell where the arguments to the exec'd program end (if there are any) and additional arguments to "find" begin.

Andrew Medico
+3  A: 

I'd recommend that you instead do that as

find . -perm 777 -print0 | xargs -0 chmod 770

"xargs" says to take the results of the find and feed it 20 at a time to the following command.

Paul Tomblin
Twenty at a time? Says who?
Rob Kennedy
man xargs (of version 4.4.0) doesn't mention the number 20. This behaviour can be enforced though, by calling xargs with -n20.
Stephan202
"20 at a time" was sort of a average. It's really however many as it thinks can fit in a normal command line, which I think is 512 bytes long in POSIX.
Paul Tomblin
The byte limit is LINE_MAX, for which I'm finding the common definition to be 2048.
Rob Kennedy
Thank you! The option print0 and the command xargs are useful together.
Masi