【问题标题】:Bash: renaming file extension, getting errorsBash:重命名文件扩展名,出现错误
【发布时间】:2016-02-25 18:16:19
【问题描述】:
root@myhost# ./removedott.sh
Mon Nov 23 10:07:04 EST 2015 - Renaming tester.xml.P.T extension to xml
mv: `tester.xml.P.T' and `tester.xml' are the same file

删除了.sh:

if [[ $(find . -type f -name "*.T"  | wc -l) -gt 0 ]]
        then
              for f in *.T
                        do
                                echo "`date` - Renaming $f extension to xml"
                                y=${f%%.*}
                                mv -v -- "$f" "${y}.xml"
                                done
fi

我不知道为什么会出现这个错误,我意识到文件是一样的..这就是全部目的..我只是想重命名它。

【问题讨论】:

  • 您的目录中是否已有tester.xml 文件? (此外,find 测试可能更好地替换为仅启用nullglob 和/或测试*.T 作为循环中$f 的值。)
  • 在我做的目录中:触摸 tester.abc.T 然后运行脚本,在第一次通过时它会删除 .abc.T 并将其重命名为 tester.xml。如果我然后 mv tester.xml 到 tester.abc.T 并再次运行脚本,那就是我得到错误的时候
  • 如果它已经以您想要的名称存在并且您知道它,为什么要重命名它?我闻到了XY Problem
  • @EtanReisner 如果它已经存在,为什么不直接覆盖?
  • tester.xmltester.xml.P.T 是否可能已经是同一个文件的两个硬链接?运行ls -li 并检查每个文件的第一列。如果是同一个数字,他们已经是同一个文件了。

标签: bash rename


【解决方案1】:

这不是为什么您的 mv 不起作用的答案,而是执行相同操作的脚本的另一个版本:

#!/bin/bash
# Renames ".T" extensions to ".xml" extensions for files in the current
# directory and subdirectories
for file in $(find . -type f -name "*.T"); do
    echo "$(date) - Renaming $file extension to .xml"
    mv -v $file $(echo $file | sed 's/\.T$/\.xml/g')
done

这对我有用。希望对您有所帮助!

【讨论】:

【解决方案2】:

所有这些解决方案,甚至我的工作都很好。这最终成为在重命名期间影响文件的主机上的一个进程。 Linux 在此期间找不到该文件。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2011-01-11
    • 2015-01-06
    相关资源
    最近更新 更多