clonefile
v3.11.0
Published
Clone file into multiple locations
Readme
Installation
npm install clonefile [-g]Usage
In a Terminal
$> clonefile <source> <target> [options] $> clonefile [--source] <source> [--target] <target1> <target2>...<targetN> [options] $> clonefile --sources <pattern> <target1> <target2>...<targetN> [options] Programmatically
via CJS
const {cloneFile} = require("clonefile");
// "Source" and "target" can be strings or arrays of strings
cloneFile(source, target, {silent: true, force: true})or via ESM
import {cloneFile} from "clonefile";
cloneFile(source, target, {silent: true, force: true})Examples
Make a faithful copy with --clone
Use --clone when a backup or handoff needs the original file's contents, access
time, and last-modified time. The copy also preserves permission bits and ownership
on Linux and other POSIX systems. On Windows, it preserves the creation time and
read-only setting. Windows creation-time preservation uses Windows PowerShell.
Make one backup copy
clonefile report.pdf report-backup.pdf --cloneSend the same file to two destinations
clonefile report.pdf report-backup.pdf report-for-review.pdf --cloneBoth copies get the source file's timestamps. Each copy has its own filesystem ID and can be edited independently.
From JavaScript
import {cloneFile} from "clonefile";
cloneFile("report.pdf", "report-backup.pdf", {preserveMetadata: true});Timestamp precision depends on Node.js and the destination filesystem. Preserving
ownership can require extra privileges. If applying metadata fails, Clonefile
reports a failure and the destination may already contain the copied data.
Linux does not provide a standard way to set creation time. Metadata change time
(ctime on Linux), ACLs, and extended attributes are outside the option's guarantees.
Clone a file with ACLs and extended attributes
clonefile report.pdf report-backup.pdf --clone-strict--clone-strict preserves the same timestamps and permissions as --clone, plus
access control lists (ACLs) and extended attributes. In JavaScript, use
{strictMetadata: true} with cloneFile or cloneGlobs.
- Linux requires Python 3 and copies POSIX access ACLs and extended attributes visible to the process. Protected attributes may require elevated privileges.
- Windows requires Windows PowerShell and uses the native file copy API for extended attributes and alternate data streams. It also copies the Windows access ACL (DACL). Windows audit rules (SACLs) are outside this option's scope.
If copying the covered metadata fails, the CLI exits nonzero and the API reports the target as failed. The destination may already contain copied data. The source and destination filesystems must support the relevant metadata.
Display file list in a directory without doing any copies
$> clonefile my-dir/ --list-only💻 ↴
[
"./docs/images/img_1.png",
"./docs/images/img_2.png",
"./docs/images/img_3.png",
"./docs/images/img_4.png",
...
]Clone a file
$> clonefile license.txt license.md or
$> clonefile --source license.txt --target license.md NOTE: The source and the target arguments can be omitted
Clone a file into directory
# Copy license.txt into my-dest-dir
$> clonefile license.txt my-dest-dir/ Clone a file to multiple locations
# Copy license.txt into my-file-target-1 (file), my-file-target-2 (file) and within (my-dest-dir-1/) (directory)
$> clonefile license.txt my-file-target-1 my-file-target-2 my-dest-dir-1/ Clone a directory in multiple directories
$> clonefile --force sourcedir/ my-dest-dir-1/ my-dest-dir-2/ my-dest-dir-3/ Clone multiple files from a glob pattern to multiple directories with the --sources options (mandatory)
$> clonefile --force --sources *.txt my-dest-dir-1/ my-dest-dir-2/ my-dest-dir-3/ Result
C:/projects/clonefile/somefile1.txt => C:/projects/clonefile/my-dir-1/somefile1.txt C:/projects/clonefile/somefile1.txt => C:/projects/clonefile/my-dir-2/somefile1.txt C:/projects/clonefile/somefile1.txt => C:/projects/clonefile/my-dir-3/somefile1.txt
3 items cloned
C:/projects/clonefile/somefile2.txt => C:/projects/clonefile/my-dir-1/somefile2.txt C:/projects/clonefile/somefile2.txt => C:/projects/clonefile/my-dir-2/somefile2.txt C:/projects/clonefile/somefile2.txt => C:/projects/clonefile/my-dir-3/somefile2.txt
3 items cloned
C:/projects/clonefile/somefile3.txt => C:/projects/clonefile/my-dir-1/somefile3.txt C:/projects/clonefile/somefile3.txt => C:/projects/clonefile/my-dir-2/somefile3.txt C:/projects/clonefile/somefile3.txt => C:/projects/clonefile/my-dir-3/somefile3.txt
3 items cloned
Notes
NOTE: The --force option is required for cloning a directory
To help clonefile knows whether you want to copy a file to a directory or another file, always add a trailing slash "/" to folders.
Clone files using multiple glob patterns to multiple directories
$> clonefile --force --sources *.txt --sources ./sowehere/ --sources ../another-location/*.png my-dest-dir-1/ my-dest-dir-2/ my-dest-dir-3/ Clone multiple files and folders from multiple locations and files and folders from multiple glob patterns to multiple directories
$> clonefile --force --source my-file-1 --source path1/my-file-2 --source path1/my-dir/ --sources *.txt --sources /another-location/*.png my-dest-dir-1/ my-dest-dir-2/ my-dest-dir-3/ "my-file-1" and "path1/my-file-2" are regular files and will be copied to "my-dest-dir-1/", "my-dest-dir-2/", "my-dest-dir-3/"
"path1/my-dir/" is a directory that will be copied to "my-dest-dir-1/", "my-dest-dir-2/", "my-dest-dir-3/"
"*.txt" will copy all .txt in the current directory to "my-dest-dir-1/", "my-dest-dir-2/", "my-dest-dir-3/"
"/another-location/*.png" my-dest-dir-1/ will copy all .png in /another-location to "my-dest-dir-1/", "my-dest-dir-2/", "my-dest-dir-3/"
Options
| Options | default | Expect | Description | |------------------------|-------------|------------|----------------------------------------------------------------------------------------------| | --silent | false | boolean | Whether to display messages | | --sources | "" | string | Glob Pattern to determine files to copy | | --source | "" | string | Regular file path for files or directories to copy | | --target | "" | string | Destination files or folders | | --recursive | false | boolean | create target directories if nonexistent | | --force | false | boolean | Allow cloning directories & Overwrite destination | | --progress | false | boolean | Display a progress bar during copy | | --clearProgress | false | boolean | Remove the progress bar when task complete | | --list | false | boolean | Print out the file list to copy | | --list-only | false | boolean | Only display the file list to copy in an array formatted for Js. No copy will be done | | --dry | false | boolean | Print out all operations, but without making any copies | | --clone | false | boolean | Preserve timestamps and supported permissions, including creation time on Windows | | --clone-strict | false | boolean | Preserve the same metadata as --clone, plus access ACLs and extended attributes | | --no-limit | false | boolean | Ignore the limit set on copies (10000 by default). | | --hide-overwrite-error | false | boolean | Don't display error messages on overwrite. |
--progress

Package
📁 package
│
│─ 📝 index.cjs (2.1Kb)
│─ 📝 index.mjs (2.3Kb)
│
└───📁 src
│ │
│ │─ 📝 clonefile.cjs ⇽ CJS version - Node (25.1k unminified)
│ │─ 📝 clonefile.esm ⇽ ESM version - Node (25.7k unminified)Changelog
current:
- Fix --recursive option ignored when invoked programmatically
3.8.2:
- Restore the --recursive option
3.8.0:
- Add the option --hide-overwrite-error
3.7.2:
- Allow copying with clonefile and clone functions
3.7.0:
- Follow symlinks
3.6.0:
- Generate code for ESM with to-esm
- Fix help command not working
3.5.0:
- Replace the --force option with --no-limit to allow unlimited copies
- Increase the default limit for copying at once to 10000 (Use --no-limit to remove limitation)
- Copy with no limit when cloning programmatically and add a --no-limit option (cli) to enforce unlimited copies
- Add --list, --list-only and --dry options
3.4.0:
- Add the option --clearProgress
3.3.0:
- Add a --progress option to display a progress bar while copying
3.2.6:
- Copy symlinks
3.2.5:
- Fix --force option not working with cloneGlobs
3.2.4:
- Review file limits processing (5000 instead of 200) (To increase or reduce that limit, create an environment variable called CLONE_FILE_MAX_PATTERN and pass the new limit)
3.2.3:
- Increase limit files to copy from 200 to 5000
- Fix js error in cloneGlobs
3.2.1:
- Use clone and cloneGlobs for programmatic copies
3.2.0:
- Import the clone function to copy from code
3.1.0:
- Add various fixes
- Deprecate the --recursive option with --force
- Allow selecting multiple sources with the --source option
2.1.1:
- Do not stop at the first error
1.1.3:
- Make --force option automatically be --recursive and --overwrite
1.1.0:
- Allow cloning a directory
- Replace the --verbose option with --silent
- Fix cloning when options misplaced
