Monday, September 5, 2022

[SOLVED] How do I merge one directory into another using Bash?

Issue

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 from html_new/a/b2.html
html/index.html was kept untouched


Solution

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.)



Answered By - Luke Maurer
Answer Checked By - Terry (WPSolving Volunteer)