NVI NVI - 6 months ago 10
Bash Question

How do I merge one directory into another using Bash?

I'm looking for shell script that merge files from one directory into another.

Sample:

html/
a/
b.html
index.html

html_new/
a/
b2.html
b.html


Usage:

./mergedirs.sh html html_new


Result:

html/
a/
b.html
b2.html
index.html


html/a/b.html
was replaced by
html_new/a/b.html


html/a/b2.html
was copied to
html_new/a/b2.html


html/index.html
kept untouched

Answer

You probably just want cp -R $1/* $2/ — that's a recursive copy.

(If there might be hidden files (those whose names begin with a dot), you should prefix that command with shopt -s dotglob; to be sure they get matched.)