Have you ever got this message when moving a file? mv: will not overwrite just-created Have you ever got this message when moving a file? mv: will not overwrite just-created shell shell

Have you ever got this message when moving a file? mv: will not overwrite just-created


Here's how to reproduce it:

> mkdir a b c> touch a/file> touch b/file> mv a/file b/file c/mv: will not overwrite just-created `c/file' with `b/file'

There may be other ways to reproduce this, but it's reasonable to assume above has happened.

That is, your script moved multiple files with the same name into the same target in one single mv command. After executing the above you will notice that a/file was successfully moved (and b/file left as is), so next time you execute it, the problem will most likely go away.


It happens because two different files with the same name would be moved to the same place with only one command. The -f option won't help for this case, it only applies when there already is a target file that will be overwritten when running the mv command. What occurs is that one of the files (the first encountered) by mv is moved, and it refuse to move the second one (that would overwrite the other file at risk of lousing user data). This behavior also explains that if you have only two files with the same name the warning will disappear the second time you are running the command.

However if you have many files with the same name in your directory tree the warning can stay there for many runs.

If you know what you are doing a way to avoid this warning is adding the option --backup=numbered to mv. The target files won't be overwritten but backup files created wheever collision occurs. If the idea is to remove these it can easily be done afterward using rm *~.