I have two folders /var/first/app and /var/second/app. I have different files within both folders and few are same. I want to merge /var/second/app to /var/first/app. How can I do that?

  • copy them recursively with cp -rf (it'll overwrite the target files) then remove the older directory files. – v_sukt Jun 30 '17 at 13:01
  • Do you have same files within the same folders? – Ravexina Jun 30 '17 at 13:02
  • 1
    @Ravexina: yes, if they are same then in similar folder structure – Purushotam Sangroula Jun 30 '17 at 13:03
  • To get good answers, you will need to be specific about exactly what you want to happen for the "few are same" – steeldriver Jun 30 '17 at 13:14
  • What do you mean "merge"? What exactly do you want to happen to files that appear in both directories with identical names? – David Foerster Jun 30 '17 at 14:16

This should do the trick:

rsync -av /var/second/app /var/first/app
| improve this answer | |

Use something like:

cp -r /var/first/app /var/second/
rm -r /var/first/app

or change cp -r to cp -a to preserve ownership and timestamps.

You can also use -i to make sure what is going on. it's going to prompt you before overwriting anything.

| improve this answer | |

You may first backup your destination folder (just in case) :

cp -r /var/first/app /var/first/app.backup

If you don't care overwriting files :

cp -fr /var/second/app /var/first/app

It will copy recursively the second folder into the first one, overwriting files with same names.

If you don't want to overwrite existing files :

cp -nr /var/second/app /var/first/app

If all is ok, you can remove the backup :

rm -rf /var/first/app.backup
| improve this answer | |

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