0

I have 2 folders let's call it X and Y, inside them I have several subfolders. I need to merge the content of both, keeping all files and overwriting the duplicates in order to have just Y, with the whole content, no duplicates and without losing any file. I'd like to avoid complex scripts or using cp as I read in many other questions since I want to keep just one folder

my actual tree is

rita/
└── X/
    └── folder1
    └── folder2
    └── foo.txt
└── Y/
    └── folder1
    └── folderA
    └── bar.txt
    └── foo.txt

is

mv /X/ /Y/ 

the right command to use being in rita folder?

i want to obtain this

rita/
└── Y/
    └── folder1
    └── folder2
    └── folderA
    └── bar.txt
    └── foo.txt

or eventually what should I do to get this?

rita/
└── Y/
    └── folder1
    └── folder1 copy
    └── folder2
    └── folderA
    └── bar.txt
    └── foo.txt
    └── foo copy.txt

thanks

rita1989
  • 39
  • 7
  • 1
    Does this answer your question? [How do I merge one directory into another using Bash?](https://stackoverflow.com/questions/4572225/how-do-i-merge-one-directory-into-another-using-bash) – Martin Marconcini Oct 18 '21 at 10:06
  • not really :( it either uses cp or complex scripts that just makes me go crazy :D I'd like to understand how to do that with the simple mv command – rita1989 Oct 18 '21 at 10:20
  • I just tried mv /X/folder1/* /Y/folder1/ but got "no matches found" then tried mv /X/folder1/ /Y/folder1/ and got "no such files or dir" but I do have them... what do I do wrong? – rita1989 Oct 18 '21 at 10:31
  • Might look into `rsync`. – Shawn Oct 18 '21 at 10:37
  • Ok, perhaps you want [to look at this other result instead](https://unix.stackexchange.com/a/127713/338820) (note: `mv` on unix/linux *does not merge* folder contents, hence why the "complex scripts" or `rsync` usage). (note that this question belongs on that site rather than SO, considering you want to _avoid using scripts_ which would be closer to a Programming question). – Martin Marconcini Oct 18 '21 at 13:06
  • very interesting... so I guess rsync or cp are my answer... but one doubt: the command cp has -n option which is does not overwrite. but what does it happen to the file that is not overwritten? let's say there source/a.txt and dest/a.txt. with the n option, a.txt in source gets just skipped or do I get dest/a copy.txt I'd like the second option, can I do that? thanks – rita1989 Oct 18 '21 at 14:03

1 Answers1

0

According to its manpage, the Unix cp command will overwrite files unless:

  • You supply -i (for "Interactive") in which case you will be prompted to overwrite or skip (no "backup copy" option offered)
  • You supply -b (for "Backup") in which case it will append ~ to the filename at the end.

To see this in action, I did all this:

$ mkdir temp
$ cd temp
$ touch a.txt
$ touch b.txt
$ mkdir xx
$ ls
a.txt  b.txt  xx
$ cp a.txt b.txt xx
$ ls xx/
a.txt  b.txt
$ cp -i a.txt b.txt xx
cp: overwrite 'xx/a.txt'? n
cp: overwrite 'xx/b.txt'? n
$ cp -b a.txt b.txt xx
$ ls xx/
a.txt  a.txt~  b.txt  b.txt~

NOTE: This was done on a Linux machine. Because macOS ships with old binaries, the version supplied with macOS Big Sur & Monterrey (at this time in 2022), doesn't support the -b command. I haven't tested macOS Ventura.

It Just Works™ (macOS Big Sur & Monterrey)
#Big Sur
$ uname -v
Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64

#Monterrey
$ uname -v
Darwin Kernel Version 21.6.0: Thu Sep 29 20:13:56 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T6000


$ cp -b a.txt b.txt xx
cp: illegal option -- b
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • 1
    @Jody Frankowski if you don't agree with the correctness of the proposed solution, the right response is to downvote to let the author and future visitors know that you think there are problems. Don't edit the answer to remove parts you don't agree with. – Z4-tier Nov 23 '22 at 04:38
  • I have slightly updated the text to remove the _offensive towards macOS_ language and include macOS Monterrey information. – Martin Marconcini Nov 24 '22 at 09:06