Overview of tl.rename

tl.rename provides an implementation of the rename program some form of which is included with many POSIX operating systems. While those tools usually do only substring or regular expression replacement, planned file name transformations in tl.rename include:

  • substring replacement as in Gentoo’s rename implementation, for example
  • regular expression replacement as seen in Debian’s rename implementation
  • reading the new names from a file or standard input
  • various case transformations
  • additional regex replacements inserting formatted counters
  • interactive renaming using readline, if available
  • interactive renaming using an external text editor

Version 0.1 implements reading names from a file or standard input, case transformations, simple substring replacement and interactive renaming using readline.

The tl.rename package is readily usable as a library but also installs an executable script that exercises its functionality.

Usage

rename [options] [file paths]

File paths may contain directory paths and be either absolute or relative to the current working directory. The specified files do not need to exist when rename is called but will cause it to fail if they do not exist when the actual renaming is being done.

Options

-h, --help show this help message and exit
-d, --debug debug mode, do not catch Python exceptions
-D, --dry-run dry-run mode, do not touch the file system
-s SLICE, --slice=SLICE
 transform only a slice of each name, value is LOWER:UPPER, both bounds are optional
-n NAMES_FILE, --names-file=NAMES_FILE
 read new names from file where - denotes standard input
-c CASE, --case=CASE
 case-transform names, possible values are upper, lower, sentence
-r FROM_TO, --replace=FROM_TO
 where FROM_TO is two distinct arguments (-r FROM TO); globally replace first option argument with second, may be given multiple times
-i, --interactive
 edit names interactively (assumed if no transformation is explicitly specified)

How it works

tl.rename runs one or more string transformations on a sequence of file names given as command arguments, then renames the files accordingly.

Transforming file names

Which transformations to apply is determined by command line options. If multiple transformations are selected, they will be run in the following order:

  1. Read names from file (--names-file)

    Read new file names from a file or standard input, one name per line.

  2. Case transformation (--case)
    Perform a case transformation on the file names:
    • Turn the names into all upper case (upper).
    • Turn the names into all lower case (lower).
    • Apply a style of mixed case, basically capitalizing the first word of a phrase (sentence). See the doctest examples for sentence case for the complete rules.
  3. Replace substrings (--replace)

    Replace substrings of the file names where match patterns and replacements are given literally as two arguments. Any number of replacements can be made in one go.

  4. Interactive replacement (--interactive)

    Let the user edit each file name in turn using the readline library if available. This provides comfortable line editing including a history.

The list of file names must fulfill the following conditions:

  • Each old and new name is non-empty and contains no null characters.
  • The number of names remains the same during each transformation.
  • Names must be unique before and after each transformation. Trailing path separators don’t make a difference.

Renaming files

Once all transformations have been performed on the file names, those items whose names have actually changed will be renamed in the file system (except if dry-run mode is active). Renaming is subject to the following rules:

  • Files and directories to be renamed must exist at this point.
  • If a file name contains subdirectory path elements and one of those has changed, the item will be moved to the new directory.
  • When moving items between directories, directory hierarchies will be created as needed and emptied directories are removed. Empty directories themselves can be moved.
  • Renaming a file to the name of another existing file overwrites that file.
  • Renaming a directory to the name of an existing empty directory overwrites that directory.
  • Directories cannot be renamed to existing populated directories or non-directories, nor can non-directories be renamed to existing directories.
  • Renaming an item to the name of another item that is renamed in the same run does not overwrite that item. In particular, it is possible to interchange the names of two items immediately.
  • Symbolic links are never followed.

Slicing

It is possible to restrict transformations to a specified portion of each file name. As an example, this can be useful when applying sentence case to prefixed file names if the prefix should not count as the beginning of a sentence.

Which portion (or slice) of each file name to subject to transformations is determined by a specification given as the value of the --slice option. The syntax of this value is that of Python’s simple slices: two integer numbers separated by a colon. The numbers denote the start and stop index of the slice where counting starts from 0. The stop index is the index of the first character after the slice. Both start and stop index may be omitted (but the colon may not); they default to the beginning and the end of the name (0 and a very big number), respectively. Negative indexes are understood to count from the end of the name.

Let’s give some examples, applied to the file name “05 - An interesting song.ogg” (28 characters):

Specification Slice
: 05 - An interesting song.ogg
5:100 An interesting song.ogg
:-4 05 - An interesting song
5:-4 An interesting song
5:24 An interesting song

Table Of Contents

Previous topic

tl.rename documentation contents

Next topic

Unit tests

This Page