Using cp to replace a directory of the same name
10,144
Solution 1
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
.
Solution 2
Asterisk does the thing ;)
cp -a test/a/* a
Author by
John Hunt
Updated on September 18, 2022Comments
-
John Hunt almost 2 years
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 withina
, it didn't replacea
. How can I makecp
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 :)
-
cuonglm almost 10 yearsThis won't copy hidden files.
-
beginer almost 10 years@Gnouc , cool...you're the man again ;) , hey but that wasnt the main point of the OPs question ;)
-
John Hunt almost 10 yearsI just tested it, although it might not replace the directory itself it does indeed work in the same manner as replacing the directory. Thanks :)