BASH 脚本更改具有相同名称但大小写不同的文件的名称
我正在尝试编写一个 bash 脚本,它将递归搜索目录,查找具有相同名称但大小写不同的文件,然后重命名它们。
例如,包含 file.txt 和 File.txt 的目录,file.txt 将保留,File.txt 将是 File.0(或任何数字,只要保留两个副本即可。)
这是我的代码,但我看到问题。用于扫描重复名称的行正在更改文件路径中的大小写,使它们无效。我想不出一种方法来检测重复项而不删除路径中的大小写。
#!/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".
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过类似
解释:
sort -f
:忽略大小写uniq -i -d -D
:忽略大小写(-i),打印重复行(-d),打印所有重复行 (-D)从那里应该很容易意识到您想要做什么。
Did you try something like
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.