Skip to content
Lloyd Brookes edited this page Jan 29, 2022 · 38 revisions

Some real-world examples.

Please note:

  • The examples are tailored for Windows users, therefore use double-quotes. Mac/Linux users must use single quotes for the examples to function correctly.
  • All examples include the dry-run -d flag to avoid any accidents caused by copy-pasting the commands.
  • These examples assume renamer is installed globally for general use on the command line (npm install --global renamer).

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

Exclude certain directories

Renamer supports standard bash pattern matching, which includes the !(pattern-list) exclusion syntax.

For example, rename .js files to .mjs files in all folders except node_modules and coverage:

$ renamer -d --find /\.js$/ --replace .mjs --path-element ext "!(node_modules|coverage)/**"

Dry run 

✔︎ bin/cli.js → bin/cli.mjs
✔︎ lib/cli-app.js → lib/cli-app.mjs
✔︎ lib/cli-args.js → lib/cli-args.mjs
✔︎ lib/config.js → lib/config.mjs
✔︎ lib/middleware-plugin.js → lib/middleware-plugin.mjs

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 (the default), 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

Replace all .mjs file extensions with .js

Real-life example of changing file extensions in a software project.

$ renamer -d --path-element ext rollup.config.mjs index.mjs bin/* lib/* test/*.mjs -f ".mjs" -r ".js"

Dry run

✔︎ rollup.config.mjs → rollup.config.js
✔︎ index.mjs → index.js
✔︎ bin/downloads.mjs → bin/downloads.js
✔︎ bin/npms.mjs → bin/npms.js
✔︎ bin/registry.mjs → bin/registry.js
✔︎ lib/downloads.mjs → lib/downloads.js
✔︎ lib/npms-api.mjs → lib/npms-api.js
✔︎ lib/registry.mjs → lib/registry.js
✔︎ test/downloads.mjs → test/downloads.js
✔︎ test/npms-api.mjs → test/npms-api.js
✔︎ test/registry.mjs → test/registry.js

Rename complete: 11 of 11 files renamed.

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

Add a conditional suffix

Add a (done) suffix to the name element only in cases where it does not already exist.

$ renamer -d --path-element name --find "/(data\d+)$/" --replace "$1 (done)" *
Before After
.
├── data1.csv
├── data2 (done).csv
├── data3.xls
.
├── data1 (done).csv
├── data2 (done).csv
├── data3 (done).xls

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 "_" *
Before After
.
├── A video.mp4
├── Another video.mp4
.
├── A vid_o.mp4
├── Anoth_r vid_o.mp4

Strip out whitespace

$ renamer -d --find "/\s/g" *
Before After
.
├── video A .mp4
├── video -  B.mp4
.
├── videoA.mp4
├── video-B.mp4

Simplify noisy filenames

$ 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 within the name path element.

$ renamer -d --path-element name --find "/\./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

{{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

Give your images a new numbering scheme, starting from n.

By default, the {{index}} value begins at 1. You can override this by specifying an --index-root value.

$ renamer -d --replace "Image{{index}}.jpg" --index-root 10 *
Before After
.
├── IMG_5776.JPG
├── IMG_5777.JPG
├── IMG_5778.JPG
.
├── Image10.jpg
├── Image11.jpg
├── Image12.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

Force mode

Changing file name case on a case-insensitive OS

On a case-insensitive system (e.g. macOS or Windows), this command (renaming file.jpg to File.jpg) will fail as the target file already exists.

$ renamer -d --find file --replace File file.jpg

You can override this behaviour by setting the --force flag.

$ renamer -d --find file --replace File file.jpg --force
Before After
.
├── file.jpg
.
├── File.jpg

Plugin Examples

First, please read the tutorial on using replace chain plugins.

Format all filenames as camel case

This example uses the renamer-case plugin to change the case of all input file names to camel-case. For the --case option, you can specify camel, kebab, lower, upper, snake or start.

$ renamer install --global renamer-case
$ renamer --dry-run --chain renamer-case --case camel lib/*

Dry run

✔︎ lib/cli-app.mjs → lib/cliApp.mjs
✔︎ lib/cli-options.mjs → lib/cliOptions.mjs
✔︎ lib/rename-file.mjs → lib/renameFile.mjs
✔︎ lib/replace-chain.mjs → lib/replaceChain.mjs

Rename complete: 4 of 7 files renamed.