Command find with exec

The below command would find any files with the extension '.doc' and copy them to your /tmp directory

# find / -name '*.doc' -exec cp '{}' /tmp/ ';'

The curly brackets are used in find to represent the current file which has been found. ie. If it found the file shopping.doc then {} would be substituted with shopping.doc. It would then continue to substitute {} for each file it finds. The brackets are normally protected by backslashes (\) or single-quotation marks ('), to stop bash expanding them (trying to interpret them as a special command eg. a wildcard). This ';' is the symbol used by find to signal the end of the commands. It's usually protected by a backslash (\) or quotes to stop bash from trying to expand it.

This finds all c-files and python files in the euler directory.
# find euler/ -iname "*.c*" -exec echo {} \; -or -iname "*.py" -exec echo {} \;

No comments: