Ahmed Z.
Ahmed Z.

Reputation: 2337

Bash: find and copy exec command not recursive

I'm running this command to find all the files named deploy.php in my whole project and make a copy of them and place them in the same directory as they were found, with name deploy_bkp.php

find . -type f -name "deploy.php" -exec cp  {} deploy_bkp.php  \; 

But its not working recursively. its only working for files on the top directory.

Can anyone help me. thank u

Upvotes: 0

Views: 100

Answers (1)

hobbs
hobbs

Reputation: 240314

Use -execdir instead of -exec. With -exec, the current directory doesn't change for each file, so the backup file is created in your starting directory, regardless of where deploy.php is found. -execdir tells find to chdir to the correct directory beforehand, so your cp will behave as expected.

Upvotes: 1

Related Questions