I want to copy some or all files from a directory test/A/ to directory test/B/, but without copying directory A itself.

I tried the command cp -R test/A/ test/B, but it copies the directory and the files as well and become test/B/A/


When you specify test/A/, you tell Bash to copy the directory itself.

To copy only the contents of a directory, use the * glob:

cp -R test/A/* test/B

Here, * will match all files in A, except hidden ones prefixed with a dot, e.g. .htaccess. To include these as well with a *, call shopt -s dotglob before.


to get an identical copy locally or remote I would prefer rsync over cp. This gives you much finer control over what should be copied or even deleted either on source or destination if so desired. And by the way fixes issues with hidden files (aka .*). Basic usage is as simple as with cp though.

to copy a directory A and it's contents to B use:

rsync -va test/A test/B

to copy the contents of directory A (including hidden files) to B use:

rsync -va test/A/ test/B

if B does not exist it's created

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.