safe-rename-cli

Simple and safe command-line renaming utility using JavaScript RegExps

Usage no npm install needed!

<script type="module">
  import safeRenameCli from 'https://cdn.skypack.dev/safe-rename-cli';
</script>

README

Rename npm

A simple and safe command-line renaming utility using JavaScript regular expressions.

Prevents renaming collisions and overwriting existing files. Checks for collisions between input files before beginning to rename them, so you don't end up with a dirty directory.

No runtime dependencies outside of Node.js >=v8.

Installation

$ npm install -g safe-rename-cli

The executable is aliased to rename and rename.js.

Usage

 rename.js [OPTION]... PATTERN REPLACEMENT FILE...

 -h, --help              Show this help.
 -v, --verbose           Print extended information.
 -d, --dry-run           Don't modify any file.
 -C, --ignore-collisions Force rename on collision conflicts.
 -S, --skip-problematic  Continue renaming non-problematic files instead of stopping on errors.

Examples

The syntax is similar to calling String#rename in JavaScript.

$ ls
  foo.jsx  bar.jsx  bazjsx
$ rename '\.jsx

 '.js' *
$ ls
  foo.js  bar.js  bazjsx

Capture groups and back references:

$ ls
  foobar  fooBAZbar  fooQUXbar
$ rename '([A-Z]+)bar' 'bar-$1' *
$ ls
  foobar  foobar-BAZ  foobar-QUX

Problematic renaming operation:

$ ls
  foo-10.txt  foo-11.txt  foo-9.txt
$ rename 'foo-(.)\.*' '$1-foo.log' *
  ERROR:  Colliding files:
     "foo-10.txt",
     "foo-11.txt"
  => "1-foo.log"
$ ls
  foo-10.txt  foo-11.txt  foo-9.txt
# Nothing changed, foo-9.txt wasn't touched either.

Here, we can either run rename with -S to skip the conflicting files and deal with them manually later, or we can fix the regular expression so it doesn't produce any collision, e.g.:

$ rename 'foo-([0-9]*)\.*' '$1-foo.log' *
$ ls
  10-foo.log  11-foo.log  9-foo.log

Notes