1

I have a folder on my Linux system that is cross-synchronized with other computers, some of them with Windows. The problem is that in that folder there are files that are "case duplicates", i.e. their file name is same except that one or more characters are uppercase vs. lowercase. For the Linux system this isn't a problem, but for the Windows system it is and it complains about duplicate file names.

Is there a simple command line way to find and replace such file names, something like "convert all file names to lowercase, if this results in two files with the same name append '1' to one of them"?

1

If you want to avoid re-inventing the wheel, you could use the mv command's built in ability to do automatic numbered backups; if your shell supports the case conversion natively that could be as simple as

for f in *; do mv --backup=numbered -- "$f" "${f,,}"; done

The default backup number format is .~1~, for example given

SOME FILE  sOmE fIlE  some file

then

$ for f in *; do mv -v --backup=numbered -- "$f" "${f,,}"; done
‘SOME FILE’ -> ‘some file’ (backup: ‘some file.~1~’)
‘sOmE fIlE’ -> ‘some file’ (backup: ‘some file.~2~’)
mv: ‘some file’ and ‘some file’ are the same file

If you don't like the default numbering, you could always change that after the fact; if your system includes the perl-based rename command that could be something like

$ rename -v -- 's/\.~(\d+)~/$1/' *.~*~
some file.~1~ renamed as some file1
some file.~2~ renamed as some file2

finally giving

$ ls
some file  some file1  some file2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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