Skip to content
Lloyd Brookes edited this page Jul 18, 2018 · 38 revisions

Some real-world examples.

Please note:

  • All examples include the dry-run -d option to avoid any accidents caused by copy-pasting the commands.
  • The examples are tailored for Windows users, therefore use double-quotes. Mac/Linux users must use single quotes for the examples to function correctly.

Contents

The basics

Some examples using plain text --find and --replace strings.

Simple replace

The * wildcard in this command means "all files and directories in the current working directory".

$ renamer -d --find "[bad]" --replace "[good]" *
Before After
.
├── A poem [bad].txt
├── A story [bad].txt
.
├── A poem [good].txt
├── A story [good].txt

Rename files and folders, recursively

The ** wildcard in this command matches all files and directories recursively from the current working directory downward.

$ renamer -d --find "pic" --replace "photo" "**"
Before After
.
├── pic1.jpg
├── pic2.jpg
└── pics
    ├── pic3.jpg
    └── pic4.jpg
.
├── photo1.jpg
├── photo2.jpg
└── photos
    ├── photo3.jpg
    └── photo4.jpg

Operate only on directories

If the ** pattern is followed by a /, only directories and subdirectories match.

$ renamer -d --find pic --replace photo "**/"
Before After
.
├── pic1.jpg
├── pic2.jpg
└── pics
    ├── pic3.jpg
    └── pic4.jpg
.
├── pic1.jpg
├── pic2.jpg
└── photos
    ├── pic3.jpg
    └── pic4.jpg

Strip out unwanted text

If omitted, --replace defaults to an empty string.

$ renamer -d --find "Season 1 - " *
Before After
.
├── Season 1 - A boring episode.mp4
├── Season 1 - Not boring episode.mp4
.
├── A boring episode.mp4
├── Not boring episode.mp4

Replace a specific path element

This diagram below highlights the different elements of a path. You can operate on a specific element using the --path-element option - valid values are base, name and ext.

┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
"  /    home/user/dir / file  .txt "
└──────┴──────────────┴──────┴─────┘

This example renames only the file extension.

$ renamer -d --path-element ext --find txt --replace log *
Before After
.
└── A txt file.txt
.
└── A txt file.log

Regular Expression examples

The --find option accepts a plain string or Javascript regular expression literal. These examples demonstrate using regular expressions in find expressions.

Prefix files and folders, recursively

$ renamer -d --find "/^/" --replace "good-" "**"
Before After
.
├── pic1.jpg
├── pic2.jpg
└── pics
    ├── pic3.jpg
    └── pic4.jpg
.
├── good-pic1.jpg
├── good-pic2.jpg
└── good-pics
    ├── good-pic3.jpg
    └── good-pic4.jpg

Append suffix to files and folders, recursively

$ renamer -d --find "/$/" --replace=-good --path-element name "**"
Before After
.
├── pic1.jpg
├── pic2.jpg
└── pics
    ├── pic3.jpg
    └── pic4.jpg
.
├── pic1-good.jpg
├── pic2-good.jpg
└── pics-good
    ├── pic3-good.jpg
    └── pic4-good.jpg

Case insensitive finds

$ renamer -d --find "/mpeg4/i" --replace "mp4" *
Before After
.
├── A video.MPEG4
├── Another video.Mpeg4
.
├── A video.mp4
├── Another video.mp4

Replace all matches

By default, matches are replaced only once. Use a regular expression with the g flag to replace all matches.

$ renamer -d --find "/e/g" --replace "E" *
Before After
.
├── A video.mp4
├── Another video.mp4
.
├── A vidEo.mp4
├── AnothEr vidEo.mp4

Simple filename cleanup

$ renamer -d --find "/.*_(\d+)_.*/" --replace "Video $1.mp4" *
Before After
.
├── [New]_Annoying_filename_-_3_[38881CD1].mp4
├── [New]_Annoying_filename_-_34_[38881CD1].mp4
├── [New]_Annoying_filename_-_53_[38881CD1].mp4
.
├── Video 3.mp4
├── Video 34.mp4
├── Video 53.mp4

Do something about all those full stops

This example replaces all full-stops except the final one before the extension name.

$ renamer -d --find "/\.(?!\w+$)/g" --replace " " *
Before After
.
├── loads.of.full.stops.every.where.jpeg
├── loads.of.full.stops.every.where.mp4
.
├── loads of full stops every where.jpeg
├── loads of full stops every where.mp4

If not already done, add your name to matching files

$ renamer -d --find "/(data\d)(\.\w+)/" --replace "$1 (checked by Lloyd)$2" *
Before After
.
├── data1.csv
├── data2 (checked by Lloyd).csv
├── data3.xls
.
├── data1 (checked by Lloyd).csv
├── data2 (checked by Lloyd).csv
├── data3 (checked by Lloyd).xls

{{index}} token examples

Give your images a new numbering scheme

The special {{index}} token in the --replace string will be replaced with a number, incremented every time a file is renamed. This example also highlights that if omitted, the --find value defaults to the full filename.

$ renamer -d --replace "Image{{index}}.jpg" *
Before After
.
├── IMG_5776.JPG
├── IMG_5777.JPG
├── IMG_5778.JPG
.
├── Image1.jpg
├── Image2.jpg
├── Image3.jpg

Use a custom index format

You can specify the format of the {{index}}} value by passing a printf format string to --index-format.

$ renamer -d --replace "Image{{index}}.jpg" --index-format %03d *
Before After
.
├── IMG_5776.JPG
├── IMG_5777.JPG
├── IMG_5778.JPG
.
├── Image001.jpg
├── Image002.jpg
├── Image003.jpg