Ubuntu – Recursively rename all files and folders to Title Case from terminal

batch-renamecommand line

I am a newbie and I have searched everywhere for this, I have also tried combining a lowercase renaming command with some regex to get Title Case instead of lowercase but I wasn't very successful.

This command converts everything (files + folders) inside the given folder to lowercase:

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

And this is my attempt at title case, it works, but it is not recursive:

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

Those are just some of my attempts, if there are better, shorter solutions I would pretty much like those instead.

Could you please help me out? Thanks in advance!

EDIT: I forgot to mention, my files look like this: "09 – the Road to Home – Amy MacDonald.flac"; should be renamed to "09 – The Road To Home – Amy Macdonald.flac". Notice how there are already title cased words as well as uppercase letters in the middle of a word.

Best Answer

To use the script below, you do not need more than the ability to paste :)

How to use

  1. Paste the script below into an empty file, save it as (e.g.) rename_title.py
  2. make it executable (for convenience reasons) chmod u+x rename_title.py
  3. 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.