在此线程中,我获得了关于对子目录中带有尾随空格的文件夹使用命令“重命名”的很大帮助。 如何对子目录使用“重命名”命令
再次感谢@pa4080 和@steeldriver 和@pm-b
一般来说,解决方案需要执行几次,当文件夹尾部有空格时嵌套在子目录中
当我使用一个建议的解决方案时,该解决方案本来不应该要求多次执行命令,而是自动执行,但我收到一条错误消息。
有人让我在新帖子中发布这个问题。所以我就在这里。
命令:
find . -depth -name '* ' -execdir rename -n 's/ *$//' {} +
文件结构(“好”表示末尾没有空格,“坏”表示末尾有空格):
|-FolderBad1
| |-FolderGood1
| |-FolderBad2
| | |-FolderBad3
|-FolderGood
| |-Folder Good2
| | |-FolderGood3
| | | |-FolderGood4
| | | | |-FolderBad4
| | | | |-FolderBad5
Dry-Run(标志 -n)按预期给出以下输出:
find . -depth -name '* ' -execdir rename -n 's/ *$//' {} +
'FolderBad3 ' would be renamed to 'FolderBad3'
'FolderBad2 ' would be renamed to 'FolderBad2'
'FolderBad1 ' would be renamed to 'FolderBad1'
'FolderBad5 ' would be renamed to 'FolderBad5'
'FolderBad4 ' would be renamed to 'FolderBad4'
删除 -n 标志会出现以下错误消息:
find . -depth -name '* ' -execdir rename 's/ *$//' {} +
Can't rename 'FolderBad3 ' to 'FolderBad3': No such file or directory
Can't rename 'FolderBad2 ' to 'FolderBad2': No such file or directory
Can't rename 'FolderBad5 ' to 'FolderBad5': No such file or directory
Can't rename 'FolderBad4 ' to 'FolderBad4': No such file or directory
现在再次运行 Dry-run:
find . -depth -name '* ' -execdir rename -n 's/ *$//' {} +
'FolderBad3 ' would be renamed to 'FolderBad3'
'FolderBad2 ' would be renamed to 'FolderBad2'
'FolderBad5 ' would be renamed to 'FolderBad5'
'FolderBad4 ' would be renamed to 'FolderBad4'
重新运行不带 -n 的命令会给出相同的错误消息:
find . -depth -name '* ' -execdir rename 's/ *$//' {} +
Can't rename 'FolderBad3 ' to 'FolderBad3': No such file or directory
Can't rename 'FolderBad2 ' to 'FolderBad2': No such file or directory
Can't rename 'FolderBad5 ' to 'FolderBad5': No such file or directory
Can't rename 'FolderBad4 ' to 'FolderBad4': No such file or directory
因此,只有 FolderBad1 被重命名,而子目录中的其他“坏”文件夹均未重命名。因此,这也许是我之前问过的一个类似问题,即如何将此命令应用于子目录。
有很多文字,我希望这能澄清一些问题。
你好,克里斯