I go with the following in bash

cp -r "/wordpress/3.0.1/" "/mySite/"

and the result is /mySite/3.0.1/ but I don't want nor understand why the 3.0.1/ appears in the target destination.

What am I doing wrong?


cp -r /wordpress/3.0.1/* /mySite

  • That won't copy any file in /wordpress/3.0.1/ whose name begins with a .. Better use cp -r /wordpress/3.0.1/. /mySite/. – Gilles 'SO- stop being evil' Nov 5 '10 at 22:06
  • @Gilles I'll give it a try, tx – fabjoa Nov 5 '10 at 23:12

This is a deviation from the POSIX specification, which states that

A pathname that contains at least one non-slash character and that ends with one or more trailing slashes shall be resolved as if a single dot character ( '.' ) were appended to the pathname.

But under Linux (i.e. with the cp command from GNU coreutils, cp -r foo/ bar/ acts like cp -r foo bar/ rather than cp -r foo/. bar/ when foo is a directory¹. (GNU coreutils isn't the only culprit, I just observed the same behavior in OpenBSD which is usually good at standards compliance.)

You can run cp -r "/wordpress/3.0.1/." "/mySite/" (i.e. end the source with /.) to avoid the 3.0.1/ level. Another possibility is rsync -r "/wordpress/3.0.1/" "/mySite/".

As an aside, I recommend getting into the habit of cp -a rather than cp -r if you don't use unices other than Linux and Cygwin. When you notice the difference, -a (which preserves permissions and symbolic links) is usually the right one. On other unices, use cp -Rp. With rsync, use -a.

  • Thanks! Is the use of double quotes to capture the path important? – fabjoa Nov 5 '10 at 23:15
  • @fabjoa: Here the double quotes are not necessary. You will often find them in (well-written) scripts, because they are necessary around variable substitutions (e.g. cp -- "$1" "$destination"). When the file name is specified directly, quotes are necessary only if the name contains characters that are special to the shell, such as spaces; single quotes (apostrophes) 'foo' would be more common because they protect everything except single quotes. (Shell quoting rules are complicated.) – Gilles 'SO- stop being evil' Nov 6 '10 at 0:26
  • @Gilles I'm confused by "cp -r foo/ bar/ acts like cp -r foo bar/ rather than cp -r foo bar/" - I'd expect that the last two should be different? – Volker Siegel Jul 24 '14 at 4:15
  • @VolkerSiegel I meant “rather than cp -r foo/. bar/”. Fixed, thanks. – Gilles 'SO- stop being evil' Jul 24 '14 at 7:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.