The rename
tool that ships with Ubuntu is pretty hot at this sort of stuff. You can embed little Perl expressions like so:
rename -n 's/.+/our $i; sprintf("AS%d.txt", 1+$i++)/e' *
That will just show you what it would do. Remove the -n
to make it a live-fire exercise.
This basically works by rough-defining a variable and incrementing it each file we hit. If you're going to have more than 10 files, I suggest using some zero-padding in the sprintf
. The following is good up to 999
:
rename -n 's/.+/our $i; sprintf("AS%03d.txt", 1+$i++)/e' *
... But that's easy enough to expand.
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.
Best Answer
If you put your code into its own file, make it executable, and place in your binary path (e.g. ~/bin).
e.g. rename_flac_tracks_from_meta.sh
You could then use find and xargs to recursively go through your directories and apply your script to each one.
Bear in mind that will crawl into directories that might not have flacs in, but it should just hiccup rather than die.