2

I am trying to copy a features[30,55].R script from my desktop into all the subdirectories that contain a file whose name is DONE.

I tried the following command:

find . -name "DONE"  -exec sh  'cp /Users/percyli/Desktop/features[30,55].R {}' \;

however, it gives you the following error:

sh: cp /Users/percyli/Desktop/features[30,55].R ./DONE: No such file or directory
sh: cp /Users/percyli/Desktop/features[30,55].R ./F3/F3-1/DONE: No such file or directory

where ./F3/F3-1 is one of the folders that contains the "DONE" file.

I also tried:

find . -name "DONE"  -exec cp /Users/percyli/Desktop/features[30,55].R {} \;

It does not spit out an error but nothing actually happens after I run this command.

What might be the problem here and how can I fix this?

Kusalananda
  • 333,661
Percy
  • 21

1 Answers1

1
f='/path/to/features[30,55].R'

find . -type f -name DONE \
    -exec sh -c 'echo cp "$1" "$(dirname "$2")"' sh "$f" {} ';'

This correctly calls sh with a script that copies the named file to the directory of the found DONE file.

Remove the echo once you are sure it's doing the correct thing.

Alternatively,

find . -type f -name DONE \
    -execdir cp "$f" . ';'

... if your find has -execdir.


Your issue was threefold:

  1. The error comes from leaving -c out from sh -c, which means sh was trying to execute a script called cp /Users/perc... (all as one single name). It did not find this file.

  2. The subshell also has improper quoting of the filename. Since it contains filename globbing characters, it needs to be quoted.

  3. Putting {} inside the script is potentially dangerous, or can at least have confusing consequence. See for example Why does Solaris 10 find / -exec sh -c "echo {}" \; print "{}" instead of filenames?

Your second try will overwrite the DONE files with the contents of the R script. Here too, you should quote the filename of the R script (this won't fix it though).

Kusalananda
  • 333,661