Did you receive this message while moving the file? mv: will not overwrite newly created

I have a bourne script shell that performs several tasks. One of these tasks is to move some files to a specific directory. Today, when I ran the script, I received the following message:

mv: will not overwrite just-created <filename> with <sameFilename> 

where filename is the original file name with the full path, and sameFilename is the exact same file and path. I use this script regularly every day and have never received this message before.

Immediately after running the script, I run it to check if the error persists, and I could not reproduce it again. I am running this script in Red Hat 5 Enterprise.

+8
overwrite shell move redhat
source share
2 answers

Here's how to reproduce it:

 > mkdir abc > 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 is reasonable to assume that this has happened.

That is, your script has moved multiple files with the same name to the same target in the same mv command. After doing the above, you will notice that a/file was successfully moved (and b/file left as it is), so the next time you run it, the problem will most likely disappear.

+4
source share

This is because two different files with the same name will be moved to the same location with only one command. The -f option does not help in this case, it is applied only when the target file already exists, which will be overwritten when the mv command is run. What happens is that one of the mv files (the first one met) is moved, and it refuses to move the second (which can overwrite another file, at the risk of losing 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 run the command.

However, if you have many files with the same name in the directory tree, the warning may remain there for many runs.

If you know what you are doing to avoid this warning, add the --backup=numbered option to mv. Target files will not be overwritten, but backup files are created when a collision occurs. If the idea is to remove them, this can easily be done after using rm *~ .

+1
source share

Source: https://habr.com/ru/post/650974/


All Articles