Search code examples
linuxmv

Linux move files without replacing if files exists


In Linux how do I move files without replacing if a particular file already exists in the destination?

I tried the following command:

mv --backup=t <source> <dest>

The file doesn't get replaced but the issue is the extension gets changed because it puts "~" at the back of the filename.

Is there any other way to preserve the extension but only the filename gets changed when moving?

E.g. test~1.txt instead of test.txt~1

When the extension gets replaced, subsequently you can't just view a file by double clicking on it.


Solution

  • @aandroidtest: if you are able to rely upon a Bash shell script and the source directory (where the files reside presently) and the target directory (where you want to them to move to) are same file system, I suggest you try out a script that I wrote. You can find it at https://github.com/jmmitchell/movestough

    In short, the script allows you to move files from a source directory to a target directory while taking into account new files, duplicate (same file name, same contents) files, file collisions (same file name, different contents), as well as replicating needed subdirectory structures. In addition, the script handles file collision renaming in three forms. As an example if, /some/path/somefile.name.ext was found to be a conflicting file. It would be moved to the target directory with a name like one of the following, depending on the deconflicting style chosen (via the -u= or --unique-style= flag):

    • default style : /some/path/somefile.name.ext-< unique string here >
    • style 1 : /some/path/somefile.name.< unique string here >.ext
    • style 2 : /some/path/somefile.< unique string here >.name.ext

    Let me know if you have any questions.