mvmv
v0.9.9
Published
Batch moving and renaming files using globbing patterns.
Downloads
68
Maintainers
Readme
mvmv
A NodeJS package that performs batch renaming and moving of files, with support for globbing wildcards *
and ?
.
You can use the package by importing it into your NodeJS scripts or as a command on the terminal.
Note:
Go on github for the latest version of this documentation.
Motivation
The typical implementations of the linux mv command do not support this rather intuitive usage:
> mv *.txt *.old
Installation
Installation is straightforward, as with most NPM packages.
Install mvmv
globally to use it as a command line tool anywhere on the command console:
> npm install -g mvmv
Install it as a dependency to use it in your code:
> npm install --save mvmv
In-code Usage
mvmv.exec(src, dst, cb)
accepts a callback and returns the number of successful files moved (returns NULL if file not found).
The callback is invoked after a move/rename attempt on an individual file. The arguments passed to the callback are: error
, oldPath
, newPath
, index
. index
is the zero-based position of the file in the batch of files to be processed.
Example
const mvmv = require('mvmv').create();
mvmv.exec('*.txt', 'temp/*.old');
:baby: New in v0.9.9
:
mvmv.execAsync(src, dst)
is the async counterpart of mvmv.exec()
. execAsync()
returns a Promise. The Promise resolves to the number of files successfully moved. In case of errors, the Promise rejects to an Array of Error.
Example
const mvmv = require('mvmv').create();
mvmv.execAsync('*.txt', 'temp/*.old')
.then(res => console.log(`Moved ${res} files.`))
.catch(errors => errors.forEach(e => console.log(e)));
// In an async function
async function moveFiles(src, dst) {
try {
console.log(await mvmv.execAsync(src, dst));
}
catch (errors) {
errors.forEach(e => console.log(e));
}
}
Command-line Usage
With package installed globally:
> mvmv '*.txt' 'temp/*.old'
Execute the mvmv
command without argument to display the usage information:
Usage: mvmv [options] <source> <target>
mvmv command moves (or renames) files specified by <source> to destination names specified by <target>.
mvmv supports * and ? globbing wildcards for specifying file names pattern.
If wildcards are used, <source> and <target> must be wrapped in quotes.
Multiple consecutive * wildcards in <source> are treated as one single * wildcard.
The file will not be moved if a file with the same name already exists at the target location.
Options:
-V, --version output the version number
-i, --interactive Prompts for confirmation before each move operation.
-s, --simulate Dry-runs the move operations without affecting the file system.
-v, --verbose Prints additional operation details.
-h, --help output usage information
Tip!
You can invoke
mvmv
without prior installing it by using npx (bundled with NPM since v5.2.0):> npx mvmv '*.txt' 'temp/*.old'
Caveats
mvmv
does not overwrite existing files.mvmv
treats consecutive*
wildcard in the source glob pattern as a single*
. (This does not apply to the destination glob pattern.)- globbing patterns on the command line must be enclosed in quotes to prevent wildcard expansion by the shell. An alternative is to turn shell globbing off. See StackOverflow thread for more information.
How It Works
Wildcards in the destination glob pattern correspond to the wildcards of the same type appearing in the source glob pattern, matched by the order in which they appear.
Example
mvmv '**-*-lines-*?-*?.txt' '*_*-lines-s?e?.txt'
| | | | | | | |
| | | +-------|-|--------|-+
| | +----------|-|--------+
| +-------------------|-+
+----------------------+
(Reminder: consecutive *
wildcards in the source glob pattern are treated as a single *
.)