Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I make cp replace a directory of the same name without removing the existing directory first? cp's default behaviour is to copy the source directory into the destination rather than replace it:

mkdir -p test/a
mkdir a
cp -a test/a a

a is now within a, it didn't replace a. How can I make cp replace directories? I want it to work the same way it does with files.

I could of course delete the target first, but I don't want to have to run more than one command :)

share|improve this question
up vote 6 down vote accepted

Use a dot . after a:

cp -a test/a/. a

It actually does not replace a as you though. It just copy test/a content to directory a.

share|improve this answer
I just tested it, although it might not replace the directory itself it does indeed work in the same manner as replacing the directory. Thanks :) – John Hunt Sep 1 '14 at 13:11

Asterisk does the thing ;)

cp -a test/a/* a
share|improve this answer
This won't copy hidden files. – cuonglm Sep 1 '14 at 13:00
@Gnouc , cool...you're the man again ;) , hey but that wasnt the main point of the OPs question ;) – beginer Sep 1 '14 at 13:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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