Copy File

Copy files or directories to another location

Overview

This node copies files or directories from a specified source path to a destination path. It supports copying single files or entire directories, with options to control overwriting existing files, preserving timestamps, creating destination directories if missing, and recursive copying of directory contents.

Common scenarios include:

  • Backing up files or folders to another location.
  • Moving data between different parts of a filesystem.
  • Preparing files for further processing by duplicating them into working directories.

For example, you can copy a log file from /var/log/app.log to /backup/logs/app.log, optionally preserving the original timestamps and creating the backup directory if it doesn't exist.

Properties

Name Meaning
Source Path The path of the file or directory to copy.
Destination Path The destination path for the copied file or directory.
Copy Options Collection of options controlling the copy behavior:
- Overwrite Existing Whether to overwrite existing files at the destination (true/false).
- Preserve Timestamps Whether to preserve original file timestamps (true/false).
- Create Destination Directory Whether to create the destination directory if it does not exist (true/false).
- Copy Mode What type of item to copy: "File Only", "Directory Only", or "Auto Detect" (automatically detect type).
- Recursive (Directories) Whether to copy directories recursively with all their contents (true/false).

Output

The node outputs an array of JSON objects, each representing the result of a copy operation for an input item. Each output JSON contains:

  • success: Boolean indicating if the copy succeeded.
  • copied: Boolean indicating if the file/directory was actually copied.
  • sourcePath: Resolved absolute source path.
  • destinationPath: Resolved absolute destination path.
  • sourceType: Either "file" or "directory".
  • sourceName: Base name of the source file or directory.
  • destinationName: Base name of the destination file or directory.
  • copiedBytes: Number of bytes copied.
  • copiedSizeHuman: Human-readable size string (e.g., "1.2 MB").
  • copyTime: Time in milliseconds taken to perform the copy.
  • copiedAt: ISO timestamp when the copy completed.
  • preservedTimestamps: Boolean indicating if timestamps were preserved.
  • overwriteMode: Boolean indicating if overwriting was enabled.
  • recursive: Boolean indicating if recursive copy was used (only present if source is a directory).

If an error occurs during copying and the node is set to continue on failure, the output JSON will contain:

  • success: false
  • copied: false
  • error: Error message describing the failure.
  • sourcePath and destinationPath as provided.

The node does not output binary data.

Dependencies

  • Node.js built-in modules: fs (filesystem), path.
  • A utility module providing helper functions for recursive directory copying and formatting file sizes.
  • Requires appropriate filesystem permissions to read source paths and write to destination paths.
  • No external API keys or services are needed.

Troubleshooting

  • Source path not found: Ensure the source path exists and is accessible by the n8n process.
  • Destination already exists and overwrite is disabled: Enable the overwrite option or choose a different destination path.
  • Source is not a file/directory (based on copy mode): Verify that the source matches the selected copy mode ("file only" or "directory only").
  • Cannot copy directory without recursive option enabled: Enable the recursive option to copy directories.
  • Permission errors: Check filesystem permissions for both source and destination paths.
  • Invalid paths: Use absolute or correctly resolved relative paths to avoid resolution issues.

Links and References

Discussion