-
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.
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, operate on all files in all directories except node_modules
and coverage
:
$ renamer -d --find pic --replace photo "!(node_modules|coverage)/**"
Rename all .js
files to .mjs
files in all folders except node_modules
:
$ renamer -d --find /\.js$/ --replace .mjs --path-element ext "!(node_modules)/**"
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 |
---|---|
|
|
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 |
---|---|
|
|