-
Notifications
You must be signed in to change notification settings - Fork 30
Examples
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
).
Some examples using plain text --find
and --replace
strings.
The *
wildcard in this command means "all files and directories in the current working directory".
$ renamer -d --find "[bad]" --replace "[good]" *
Before | After |
---|---|
|
|
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 |
---|---|
|
|
If the **
pattern is followed by a /
, only directories and subdirectories match.
$ renamer -d --find pic --replace photo "**/"
Before | After |
---|---|
|
|
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
If omitted, --replace
defaults to an empty string.
$ renamer -d --find "Season 1 - " *
Before | After |
---|---|
|
|
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 |
---|---|
|
|
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.
The --find
option accepts a plain string or Javascript regular expression literal. These examples demonstrate using regular expressions in find expressions.
$ renamer -d --find "/^/" --replace "good-" "**"
Before | After |
---|---|
|
|
$ renamer -d --find "/$/" --replace=-good --path-element name "**"
Before | After |
---|---|
|
|
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 |
---|---|
|
|
$ renamer -d --find "/mpeg4/i" --replace "mp4" *
Before | After |
---|---|
|
|
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 |
---|---|
|
|
$ renamer -d --find "/\s/g" *
Before | After |
---|---|
|
|
$ renamer -d --find "/.*_(\d+)_.*/" --replace "Video $1.mp4" *
Before | After |
---|---|
|
|
This example replaces all full-stops within the name
path element.
$ renamer -d --path-element name --find "/\./g" --replace " " *
Before | After |
---|---|
|
|
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 |
---|---|
|
|
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 |
---|---|
|
|
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 |
---|---|
|
|
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 |
---|---|
|
|
First, please read the tutorial on using replace chain plugins.
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.