Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 NTFS/FAT filesystem it asks me about whether i'm gonna replace it or skip.

Don't you know is there any way to automaticly rename such files, for example, by adding '(1)' to the name of conflict file (like windows 7 does)?

share|improve this question
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. :) –  user unknown Jul 14 '11 at 19:46

3 Answers 3

up vote 12 down vote accepted

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.

share|improve this answer
Brilliant. I didn't know this option existed, and it just proved to be extremely useful. Thanks @Gilles. –  Stephen Orr May 27 at 10:52

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.

share|improve this answer
The guy never implied that he's the one who created that mess. –  Tshepang Jul 14 '11 at 14:10
Well, yes, true, but he is the one suffering the consequences ☺ –  jmtd Jul 15 '11 at 11:02

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.