BASH script to change names on files with same name but differing cases
I'm trying to write a bash script that will recursively search a directory, find files with the same names but different cases, and rename them.
For example a directory containing file.txt and File.txt, file.txt would remain and File.txt would be File.0 (Or any number as long as both copies are preserved.)
Here is the code I have, though I see the problem. The line to scan for duplicate names is changing the case in the paths to the files, making them invalid. I can't think of a way to detect duplicates without also removing the case in the paths.
#!/bin/bash
#Scan for duplicates
find $1 -name "*"| tr 'A-Z' 'a-z' | sort | uniq -d > ~/case.tmp
#Count changes
i=0
#Make changes
for line in `cat ~/case.tmp`
do mv $line $line.$i
let i++
done
#Print Results
echo "$i file(s)开发者_运维知识库 renamed".
Thanks for the help.
Did you try something like
find $1 | sort -f | uniq -i -d -D
Explanation:
sort -f
: ignore caseuniq -i -d -D
: ignore case (-i), print duplicate lines (-d), print all duplicate lines (-D)
From there it should be easy to realize what you want to do.
精彩评论