2

This creates a file in the same directy as 'some.file.bak'.

find /home/ -ipath "*/temp/some.file" -type f  -exec cp {} {}.bak \;

How to make a copy in another name such as 'another.file' in the same directory as some.file instead of 'some.file.bak'.

2 Answers2

4

find /home/ -ipath "*/temp/some.file" -type f -execdir cp {} another.file \;

You just have to change exec to execdir from the other answer (sorry I cannot post it as a comment yet).

The execdir option states, according to find's man page:

-execdir command {} +

Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.

glenn jackman
  • 25,463
  • 6
  • 46
  • 69
Isaac
  • 251
  • 1
  • 2
  • +1 I came up with a convoluted solution passing `{}` into a shell. This is an excellent answer: the solution and the documentation. – glenn jackman Sep 26 '14 at 10:38
0
find /home/ -ipath "*/temp/some.file" -type f  -exec cp {} another.file \;
Peter Lamby
  • 442
  • 3
  • 8
  • i tried that earlier but cp copy another.file into the directory i run the script not the directory as some.file which i want. Sorry, i should have elaborated same directory as some.file. – 13th Matrix Sep 26 '14 at 08:38