BASH 脚本更改具有相同名称但大小写不同的文件的名称

发布于 2024-11-30 23:18:46 字数 524 浏览 5 评论 0原文

我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-12-07 23:18:46

您是否尝试过类似

find $1 | sort -f | uniq -i -d -D

解释:

  • sort -f:忽略大小写
  • uniq -i -d -D:忽略大小写(-i),打印重复行(-d),打印所有重复行 (-D)

从那里应该很容易意识到您想要做什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文