Copy Files

Path: /src/method/copy.coffee compiled to /lib/method/copy.js

This will copy a single file, complete directory or selection from directory. It will make exact copies of the files as far as possible including times, ownership and access modes. But if some of this rights are not possible to set it will be ignored without an explicit error.

To select which files to copy and how to work you can use the following options:

  • filter - Array<Object>|ObjectFilter Rules
  • overwrite - Boolean if set to true it will not fail if destination file already exists and overwrite it
  • ignore - Boolean it will not fail if destination file already exists but skip this and go on with the next file
  • noempty - Boolean set to true to don't create empty directories while no files to copy into
  • dereference - Boolean dereference symbolic links and go into them
  • ìgnoreErrors - Boolean go on and ignore IO errors
  • parallel - Integer number of maximum parallel calls in asynchronous run (defaults to half of open files limit per process on the system)

Example:

CoffeeScript Code
fs = require 'alinex-fs' fs.copy '/tmp/some/directory', '/new/destination', (err) -> return console.error err if err console.log "Directory copied!"

Or to copy all js files and overwrite existing:

CoffeeScript Code
fs = require 'alinex-fs' fs.copy '/tmp/some/directory', '/new/destination', filter: include: '*.js' overwrite: true , (err) -> return console.error err.message if err console.log "Directory copied!"

copy()

Usage:copy(source, target, options, cb)

Parameter
  • source - String path or file to be copied
  • target - String file or directory to copy to
  • options - Object optional specifications for check defining which files to copy
  • cb - function(Error, Array<String>) optional callback with list of newly created files and directly created directories or possible Èrror:
    • Target file already exists: xxxxx

copySync()

Usage:copySync(source, target, options)

Parameter
  • source - String path or file to be copied
  • target - String file or directory to copy to
  • options - Object optional specifications for check defining which files to copy
Return
Array<String> list of newly created files and directly created directories
Throws
  • Error if anything out of order happened
    • Target file already exists: xxxxxxxxxxxxxxxxx

Debugging

This module uses thedebug module so you may anytime call your app with the environment setting DEBUG=fs:copy for the output of this method only.

Because there are mkdirs subcalls here you see the output of DEBUG=fs:* while copying a small directory:

fs:copy check test/temp/dir3 +32ms
fs:copy going deeper into test/temp/dir3 directory +1ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp/dir4? +0ms
fs:copy check test/temp/dir3/file11 +0ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp/dir4 created +0ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp/dir4? +0ms
fs:mkdirs -> directory /home/alex/github/node-fs/test/temp/dir4 was already there +0ms
fs:copy copying file test/temp/dir3/file11 to test/temp/dir4/file11 +0ms