193

I'm trying to write a Bash script that will overwrite an existing directory. I have a directory foo/ and I am trying to overwrite bar/ with it. But when I do this:

cp -Rf foo/ bar/

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.

10 Answers 10

190

You can do this using -T option in cp.
See Man page for cp.

-T, --no-target-directory
    treat DEST as a normal file

So as per your example, following is the file structure.

$ tree test
test
|-- bar
|   |-- a
|   `-- b
`-- foo
    |-- a
    `-- b
2 directories, 4 files

You can see the clear difference when you use -v for Verbose.
When you use just -R option.

$ cp -Rv foo/ bar/
`foo/' -> `bar/foo'
`foo/b' -> `bar/foo/b'
`foo/a' -> `bar/foo/a'
 $ tree
 |-- bar
 |   |-- a
 |   |-- b
 |   `-- foo
 |       |-- a
 |       `-- b
 `-- foo
     |-- a
     `-- b
3 directories, 6 files

When you use the option -T it overwrites the contents, treating the destination like a normal file and not directory.

$ cp -TRv foo/ bar/
`foo/b' -> `bar/b'
`foo/a' -> `bar/a'

$ tree
|-- bar
|   |-- a
|   `-- b
`-- foo
    |-- a
    `-- b
2 directories, 4 files

This should solve your problem.

8
  • 29
    just in case anyone is tripped up by this it won't work with OSX cp developer.apple.com/library/mac/documentation/Darwin/Reference/…
    – dnfehren
    Mar 18, 2016 at 19:07
  • 19
    It's not clear that this answer is what the OP is looking for, although the examples given above mask the problem... With the -T option, files that are in an existing target (bar/) but not in the source (foo/) will be left in place, so this is not what most people would consider a complete overwrite of the directory. ie. if bar/baz existed already, it would still exist afterwards...
    – robo
    Dec 19, 2016 at 21:40
  • 5
    This answer does answer the op question, but doesn't address the case of if the destination already exists and you want to remove contents it contains but the source directory does not. This is not an expected behavior of copying files from one place to the other. It only overwrites in the target things that are also in the source, it doesn't touch anything in the target that is not in the source. You can clean the target folder by prepending a command to do it: rm -rf bar/* && cp -TRv foo/ bar/ Dec 14, 2017 at 21:08
  • 3
    I'm not a mind reader...I don't see further clarification what the OP was looking for, but this was DEFINITELY the answer that I (MEEEE) was looking for Feb 3, 2019 at 5:23
  • 4
    just in case anyone is tripped up by why the link in the most upvoted comment is not working, here it is: web.archive.org/web/20170909193852/https://developer.apple.com/…
    – Sulphur
    Feb 27, 2020 at 21:14
116

If you want to ensure bar/ ends up identical to foo/, use rsync instead:

rsync -a --delete foo/ bar/

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 in foo/ to bar/
  • --delete removes extra files not in foo/ from bar/ as well, ensuring bar/ ends up identical
  • If you want to see what it's doing, add -vh for verbose and human-readable
  • Note: the slash after foo is required, otherwise rsync will copy foo/ to bar/foo/ rather than overwriting bar/ itself.
    • (Slashes after directories in rsync are confusing; if you're interested, here's the scoop. They tell rsync to refer to the contents of the directory, rather than the directory itself. So to overwrite from the contents of foo/ onto the contents of bar/, we use a slash on both. It's confusing because it won't work as expected with a slash on neither, though; rsync sneakily always interprets the destination path as though it has a slash, even though it honors an absence of a slash on the source path. So we need a slash on the source path to make it match the auto-added slash on the destination path, if we want to copy the contents of foo/ into bar/, rather than the directory foo/ itself landing into bar/ as bar/foo.)

rsync is very powerful and useful, if you're curious look around for what else it can do (such as copying over ssh).

2
84

Do it in two steps.

rm -r bar/
cp -r foo/ bar/
10
  • 16
    This is actually the only example given so far that will ensure that bar is identical in content to foo, not a combination of items from foo plus other items that may have already existed in bar. The highly upvoted answer from @Saurabh Meshram below has this problem.
    – robo
    Dec 19, 2016 at 21:43
  • 1
    Be extra careful using this, as it will remove all files from bar, even hidden ones.
    – Elia Grady
    Jul 13, 2017 at 8:20
  • 1
    mac osx: rm -rf bar/; cp -r foo/ !$ Sep 28, 2017 at 11:44
  • 2
    This isn't always viable...or desired...imagine if foo and bar are large directories...maybe there are files in bar that are not in foo that you don't want to remove. This shouldn't be considered a realistic answer imho Feb 3, 2019 at 5:21
  • 2
    @Porcupine rsync Feb 10, 2021 at 13:29
30

Use this cp command:

cp -Rf foo/* bar/
6
  • 14
    This doesn't remove files that are present in bar but not in foo.
    – Ara
    Apr 1, 2015 at 15:00
  • 11
    Don't know what you mean by that. Why should cp command be removing file from source?
    – anubhava
    Apr 1, 2015 at 15:21
  • My understanding was that when you 'overwrite an existing directory' with another one, at the end the overwritten directory should be a copy of the other one. I.e. at the end bar should be a copy of foo, as it is the case with @jonathan-wheeler answer but if you had a file bar/c and no foo/c then bar/c doesn't get deleted. On a side note, I've just noticed that it's not the case with Saurabh Meshram's answer either.
    – Ara
    Apr 3, 2015 at 15:52
  • Maybe we don't agree on what overwrite means, for me it's basically replace, while you might meant megre ? It's hard to tell what OP wants exactly because she only mentionned 2 files that are both in foo and bar.
    – Ara
    Apr 3, 2015 at 16:02
  • 2
    This syntax also ignores hidden dot files. A large number can also blow up the glob. Aug 5, 2020 at 13:20
18

The following command ensures dotfiles (hidden files) are included in the copy:

$ cp -Rf foo/. bar
7
  • 1
    Is this true? Looks unusual.
    – Mateng
    Dec 2, 2016 at 12:51
  • 3
    @Mateng I just tested it - yes it is true.
    – Matteo B.
    Sep 11, 2017 at 14:47
  • 1
    . means all the files in the directory. So, naturally the hidden files would be included. Sep 26, 2018 at 2:56
  • That's pretty neat actually. You can also do "cp -RTf foo bar" on Linux. Aug 5, 2020 at 13:22
  • . (dot) just means the current directory, no?
    – JohnyTex
    Oct 25, 2021 at 9:09
7

Very similar to @Jonathan Wheeler:

If you do not want to remember, but not rewrite bar:

rm -r bar/
cp -r foo/ !$

!$ displays the last argument of your previous command.

1
  • 6
    The tip about the !$ is awesome!
    – Lucas
    Jan 19, 2018 at 15:05
3

this should solve your problem.

\cp -rf foo/* bar/
1
0

In case you're in Git Bash for Windows:

robocopy path/to/source path/to/target -MIR

-MIR means mirror, it'll result in two identical folders. Existing files will be skipped, extra files will be deleted.

-1

The operation you defined is a "merge" and you cannot do that with cp. However, if you are not looking for merging and ok to lose the folder bar then you can simply rm -rf bar to delete the folder and then mv foo bar to rename it. This will not take any time as both operations are done by file pointers, not file contents.

-6

Try to use this command:

cp -Rf foo/* bar/
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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