I'm trying to write a Bash script that will overwrite an existing directory. So, I have a directory foo/ and I am trying to overwrite bar/ with it. But when I do,
cp -Rf foo/ bar/
what happens is that a new bar/foo/ directory is created. I don't want that. There are two files in foo/ a and b. There are files with same names in bar/ as well. I want the foo/a and foo/b to replace bar/a and bar/b.
The following command ensures dotfiles (hidden files) are included in the copy:
If you want to ensure
bar/
ends up identical tofoo/
, usersync
instead:If just a few things have changed, this will execute much faster than removing and re-copying the whole directory.
-a
is 'archive mode', which copies faithfully files infoo/
tobar/
--delete
removes extra files not infoo/
frombar/
as well, ensuringbar/
ends up identical-vh
for verbose and human-readablefoo
is required, otherwisersync
will copyfoo/
tobar/foo/
rather than overwritingbar/
itselfrsync
is very powerful and useful, if you're curious look around for what else it can do (such as copying over ssh).You can do this using
-T
option incp
.See Man page for
cp
.So as per your example, following is the file structure.
You can see the clear difference when you use
-v
for Verbose.When you use just
-R
option.When you use the option
-T
it overwrites the contents, treating the destination like a normal file and not directory.This should solve your problem.
Very similar to @Jonathan Wheeler:
If you do not want to remember, but not rewrite
bar
:!$
displays the last argument of your previous command.Do it in two steps.
Use this
cp
command: