There is a directory YYY and a structure on the site:


Task: YYY directory to copy all directories plugins which are available on the server.

  • Apart from what have you tried, do you want to copy directory YYY to all plugin directories, or is it the other way round? – Incognito Feb 13 '13 at 10:07
  • Plus this question has nothing to do with programming. – iiSeymour Feb 13 '13 at 10:08
  • I need to copy a directory YYY (with plugin for Wordpress) in all "plugins" directories on my server. – user2067861 Feb 13 '13 at 10:36

only can copy only to one destination , so you have to use a loop or do something like

echo dir1 dir2 dir3 | xargs -n 1 cp file1

Which will copy file1 to dir1, dir2, and dir3. xargs will call cp 3 times to do this, see the man page for xargs for details.

Found at: https://stackoverflow.com/questions/195655/how-to-copy-a-file-to-multiple-directories-using-the-gnu-cp-command

  • Breaks on whitespace – Kevin Feb 14 '13 at 0:39
  • whitespace in dir name or file name ? u should be able to use "dir1" "dir2" and so on to stop that problem – Dukeatcoding Feb 14 '13 at 8:38
  • Quotes won't help for the dirs. They are interpreted by the shell and passed to exho, which prints them separated by a space. xargs then splits on whitespace, so how is it supposed to know whether the space was meant to be split on or a part of the name? – Kevin Feb 14 '13 at 13:34

cp only takes one destination at a time, so call it in a loop.

for d in */wp-content/plugins/; do
  cp -Rp YYY "$d"

Note that this snippet will keep going if any of the cp commands fail. To abort immediately on failure, run set -e first. To keep going (makes sense on permission denied, not on disk full) but still report errors, save the return status in a variable:

for d in */wp-content/plugins/; do
  cp -Rp YYY "$d" || ret=1
return $ret      # from a function; `exit $ret` in a script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy