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)?

  • Curious. I just wanted to see, what kind of error I get if I try to produce a file with the same name on a NTFS-partition (HPFS/NTFS, according to sudo fdisk -l /dev/sda), and did touch foo; touch Foo and ended with 2 files foo and Foo. But I'm not curious enogh to reboot into Windows, to look how they look like over there. Migth it just be a FAT-problem? Ah - I have an USB-Stick with FAT, and could create a FAT-system inside a file, ... - one moment please. :) Jul 14, 2011 at 19:46

3 Answers 3


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.

  • 1
    Brilliant. I didn't know this option existed, and it just proved to be extremely useful. Thanks @Gilles.
    – Steve Hill
    May 27, 2015 at 10:52
  • 2
    OSX cp does not have --backup flag. use brew install coreutils and then use the command gcp instead. Mar 21, 2016 at 19:50

I tried

apropos copy | grep "(1)" 

to find possible candidates, and mcopy showed up.

man mcopy 

shows a promising option -D clash-option isn't that cool? But not so cool - it isn't described. But there are some hints to mtools.dvi, which I searched on my system, without success, and via google, without success, but then, with google, I searched directly for mcopy clash-option and found this site.

I made a short test

mcopy -D A f* a 

to tests for autorename and targetdir a - instead of autorenaming it asked me for every file to ignore or override, that stupid s... .

My version is mtools-4.0.10 and the help page is from 1996 - 15 years old. Should we really lost some features, meanwhile?

I would split the work into two steps:

  • Make a short function, which generates a unique name for a file, if that name is occupied.
  • Run find, and execute that script for every file you wish to copy.

Shall we assist in this approach? :)

Here is a script, to autorename files:



autorename () {

test -e ${target}/${name}.$no && autorename ${name} ${target} $((no+1)) || cp ${name} ${target}/${name}.$no 


test -e ${target}/${name} && autorename ${name} ${target} 0 || cp ${name} ${target} 

and this is my test invocation:

find -maxdepth 1 -name "fo*" -type f -exec ./autorename.sh {} /mnt/hidden/test/a ";"

Note: -maxdepth, -name and -type where used to restrict the number of affected files dramatically. I didn't test the script for deeper file structures, nor for blanks in filenames and other, funky characters like linefeed, pagefeed and so on.

I used .1 because it doesn't make trouble in most commands, while a ( and a ) often need masking.


Not with GNU cp, at least.

You are strongly advised to not have duplicate file names (ignoring case), they will just cause you a world of pain. Find a list of such duplicates using

find . | tr A-Z a-z | sort | uniq -d

Then manually rename one of the files for each line if output. Try to avoid creating duplicates in future.

  • The guy never implied that he's the one who created that mess.
    – tshepang
    Jul 14, 2011 at 14:10
  • Well, yes, true, but he is the one suffering the consequences ☺
    – jmtd
    Jul 15, 2011 at 11:02
  • I have a situation where I ended up with backups from slightly different times. Want to combine them, but not clobber different versions. For instance, for all I know, the older one might be better. Can only tell after I examine. In the mean time, need to copy and rename backups.
    – abalter
    Jul 6, 2017 at 19:11
  • That's only feasible for human created files, like source code. In my case, I have a bunch of automatically generated files with the same name, because using different names would be even messier. But now I ended up needing to merge all these files, or at least put them in the same directory. Jan 6, 2021 at 18:16
  • well the point of the question is to prevent these messes from happening
    – WalksB
    Oct 2, 2022 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .