1

I've unpackaged a zip into the current location which we'll call /httpdocs, everything inside the zip resides in a directory which we'll call myfiles.

In /httpdocs are thousands of directories and files that also exist at exactly the same paths as in the myfiles directory.

I need to move the contents of the myfiles directory into /httpdocs whilst replacing any directories/files that already exist... recursively.

If I try from the myfiles directory:

mv * ../

I am asked if I want to overwrite if exists... y/n for every single file!

If I try from /httpdocs:

mv myfiles/* .

I am asked if I want to overwrite if exists... y/n for every single file!

If I try to apply the -f force operator, it returns that it cannot move because the Directory not empty.


Now from what I have read, it doesn't seem like what I am trying to do is achievable with the mv command which is probably why I am seem to be getting absolutely nowhere with this.

I am aware of cp and rsync and many others but I am not sure which command is the best to use to actually just move the contents of the directory to another location and overwrite if exists recursively.


I know I could do this easily by just uploading to /httpdocs via FTP in any local client and just 'Yes to all' on the overwrite dialogue but it would just take ages so not always practical - I'm just after a like-for-like over the command line really.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
zigojacko
  • 153
  • I am not sure what the answer is on that question @Haxiel because the accepted answer states use cp like cp -r * .. from my myfiles directory yet when I try that, I am asked y/n to overwrite for every file still. This is what I mean by I can't find something online that actually does what I am seeking. – zigojacko Nov 15 '18 at 10:55
  • cp should overwrite existing files by default. Can you check if you have an alias configured for cp in your environment? It may be mapped to cp -i or so. – Haxiel Nov 15 '18 at 10:58
  • You are spot on. which cp returns alias cp='cp -i' /bin/cp - no wonder this has been doing my head in all of this time. Will try and remove the alias and try again. Thanks. – zigojacko Nov 15 '18 at 11:03
  • For reference, the comment by @Haxiel led me to this answer... – zigojacko Nov 15 '18 at 11:04
  • Bypassed for current session with unalias cp and the cp command worked perfectly as it should do. Thank you @Haxiel for directing me to the problem (never knew about command aliases) - I do believe my question/scenario is different to that of the suggested duplicate question though and I will accept an answer if you wanted to add it. – zigojacko Nov 15 '18 at 11:14
  • Glad to know that it worked out. I'll add a brief answer here, as asked. – Haxiel Nov 15 '18 at 11:18

1 Answers1

1

As discussed under this question, the mv command does not directly provide a way to 'merge' directories by overwriting existing files.

The cp and rm commands can be used together as an equivalent alternative. In this case, the command would be:

cd /path/to/httpdocs
cp -r myfiles/* .
rm -r myfiles/

Note: The cp command overwrites existing files by default. The OP had an alias interfering with this process. The cp command was aliased to cp -i, which asks for interactive confirmation on each overwrite. This can be overridden by running unalias cp first, or using the \cp -r .. syntax.

Haxiel
  • 8,361