T.Perisic T.Perisic - 10 months ago 34
Linux Question

Explanation of every string in find's -exec command

I would like to know the meaning behind every string of this command :

find . -type f -exec chmod 644 {} \;

  • find . -type f
    means to find all files in this folder

  • -exec chmod 644
    chmod 644
    command on all of those files

Why do we need
? what are they for?

Answer Source
  • . ... start the search in current directory
  • -type f ... find files of type "file" (as opposed to "directory", "link" etc.)
  • -exec ... \; ... for every file found, run the command ...
  • chmod ... the command for exec will be chmod (change access rights for a file)
  • 644 ... the access rights will be changed to (read,write) for the owner of the file and (read) for everyone else.
  • {} ... this part will be replaced with the actual filename by find

why do we need {} and \; ? what are their meaning?

{} will get replaced with the file name found by find.

\; terminates the -exec action of find. We need it because the command in -exec can be as long as you want it to, and after that -exec, you may want to have other find actions or predicates. So you need some means of determining that one -exec part ends and some other find action follows. In practice, this is determined by one of the two terminators, ; or + with somewhat different meaning (read the man page for find). But because ; is a metacharacter in shell, we need to protect it from shell by escaping it (or putting it into quotes).