Copy files with renaming

file-copyrename

I have a huge file tree. Some files have same name but in different case, e.g., some_code.c and Some_Code.c.

So when I'm trying to copy it to an NTFS/FAT filesystem, it asks me about whether I want it to replace the file or skip it.

Is there any way to automatically rename such files, for example, by adding  (1) to the name of conflict file (as Windows 7 does)?

Best Answer

Many GNU tools such as cp, mv and tar support creating backup files when the target exists. That is, when copying foo to bar, if there is already a file called bar, the existing bar will be renamed, and after the copy bar will contain the contents of foo. By default, bar is renamed to bar~, but the behavior can be modified:

                                # If a file foo exists in the target, then…
cp -r --backup source target    #   rename foo → foo~
cp -r --backup=t source target  #   rename foo → foo.~1~ (or foo.~2~, etc)

There are other variants, such as creating numbered backups only when one already exists. See the coreutils manual for more details.

Related Question