Stats4224
Stats4224

Reputation: 808

Variables in find exec command

How does the $1 work in this find command? I can't find any examples or documentation of what this is doing anywhere. This comes from a question 'Remove all file extensions in current working dir.'

find `pwd` -type f -exec bash -c 'mv "$1" "${1%.*}"' - '{}' \;

Upvotes: 5

Views: 5036

Answers (1)

AlexP
AlexP

Reputation: 4430

The string to be executed by find is

bash -c 'mv "$1" "${1%.*}"' - '{}'

For each file it finds, find will replace {} with the pathname of the found file:

bash -c 'mv "$1" "${1%.*}"' - '/path/to/filename.ext'

bash then executes mv "$1" "${1%.*}" with $0 set to - (making it a login shell) and $1 set to /path/to/filename.ext. After applying the substitutions, this results in

mv /path/to/filename.ext /path/to/filename

Note: find `pwd` is a complicated way to say find ..

Upvotes: 9

Related Questions