Recursively rename all files and folders to Title Case from terminal
Solution 1:
To use the script below, you do not need more than the ability to paste :)
How to use
- Paste the script below into an empty file, save it as (e.g.)
rename_title.py
- make it executable (for convenience reasons)
chmod u+x rename_title.py
-
Run it with the directory to rename as argument:
/path/to/rename_title.py <directory/to/rename>
The script
#!/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)
Examples:
a file > A File
a fiLE.tXT > A File.txt
A folder > A Folder
a folder > A Folder
And more complex, excluding ["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
becomes:
Down By the River for My Uncle to Get Water.txt
etc, it simply makes all files and folders (recursively) Title Case, extensions lower case.
EDIT: I have added all articles, conjunctions and prepositions that do not need to be capitalized according to the capitalization rules for song titles.
Solution 2:
If you use find
's -exedir
, then the names get passed any command with the leading components of the pathname removed, like ./sOmE fILE
. You could then title-case each sequence of word-characters that is preceded by either the leading /
or by whitespace, e.g.
find path/ -execdir rename -nv -- 's/(?<=[\/\s])(\w)(\w*)/\u$1\L$2/g' {} +