开发者

BASH script to change names on files with same name but differing cases

开发者 https://www.devze.com 2023-03-30 09:44 出处:网络
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.

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 case
  • uniq -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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号