从终端递归地将所有文件和文件夹重命名为Title Case

我是一个新手,我已经到处寻找这个,我也尝试将小写重命名命令与一些正则表达式结合起来获得Title Case而不是小写,但我不是很成功。

此命令将给定文件夹内的所有内容(文件+文件夹)转换为小写:

while IFS= read -r -d '' file; do mv -b -- "$file" "${file,,}"; done < <(find . -depth -name '*[AZ]*' -print0) 

这是我对标题案例的尝试,它有效,但它不是递归的:

 find . -name "*.flac" -print0 | while read -d $'\0' file; do rename 's/(^|[\s\(\)\[\]_-])([az])/$1\u$2/g' *; done 

这些只是我的一些尝试,如果有更好,更短的解决方案我会非常喜欢那些。

你能帮帮我吗? 提前致谢!

编辑:我忘了提,我的文件看起来像这样:“09 – 回家的路 – 艾米麦克唐纳。弗拉克”; 应该改名为“09 – The Home To Home – Amy Macdonald.flac”。 注意一个单词中间是否有已经标题的单词以及大写字母。

要使用下面的脚本,您不需要能够粘贴:)

如何使用

  1. 将下面的脚本粘贴到一个空文件中,保存为(例如) rename_title.py
  2. 使其可执行(为方便起见) chmod u+x rename_title.py
  3. 运行它与目录重命名为参数:

     /path/to/rename_title.py  

剧本

 #!/usr/bin/env python3 import os import sys import shutil directory = sys.argv[1] skip = ["a", "an", "the", "and", "but", "or", "nor", "at", "by", "for", "from", "in", "into", "of", "off", "on", "onto", "out", "over", "to", "up", "with", "as"] replace = [["(", "["], [")", "]"], ["{", "["], ["}", "]"]] def exclude_words(name): for item in skip: name = name.replace(" "+item.title()+" ", " "+item.lower()+" ") # on request of OP, added a replace option for parethesis etc. for item in replace: name = name.replace(item[0], item[1]) return name for root, dirs, files in os.walk(directory): for f in files: split = f.find(".") if split not in (0, -1): name = ("").join((f[:split].lower().title(), f[split:].lower())) else: name = f.lower().title() name = exclude_words(name) shutil.move(root+"/"+f, root+"/"+name) for root, dirs, files in os.walk(directory): for dr in dirs: name = dr.lower().title() name = exclude_words(name) shutil.move(root+"/"+dr, root+"/"+name) 

例子:

 a file > A File a fiLE.tXT > A File.txt A folder > A Folder a folder > A Folder 

更复杂,不包括["a", "an", "the", "and", "but", "or", "nor", "at", "by", "for", "from", "in", "into", "of", "off", "on", "onto", "out", "over", "to", "up", "with", "as"]

 down BY the rIVER for my uncLE To get water.TXT 

变为:

 Down By the River for My Uncle to Get Water.txt 

等,它只是使所有文件和文件夹(递归)标题案例,扩展名小写。

编辑:我已经根据歌曲标题的大小写规则添加了所有不需要大写的文章,连词和介词。

如果使用find-exedir ,那么名称将被传递给删除了路径名的前导组件的任何命令,例如./sOmE fILE 。 然后,您可以标题化每个单词字符序列,前面是前导/或空格,例如

 find path/ -execdir rename -nv -- 's/(?<=[\/\s])(\w)(\w*)/\u$1\L$2/g' {} +