Copy and move files and folders
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-files --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-files');
This plugin is a forked version of official grunt-contrib-copy plugin (with copy
task).
Original plugin was submitted by Chris Talkington.
All credits for copy
task go to him.
The plugin was forked at v1.0.0 version of grunt-contrib-copy.
The plugin (grunt-files
) contains two tasks: copy
and move
. copy
is almost the same as in original plugin with the several additions:
- added option
forceOverwrite
- added option
skipExisting
- added option
removeSource
See Options for details.
Addiionally plugin provides move
task for moving files and folder with the same logic as copy
task.
Special note about the fork. I'm not fully sure that it was correct to fork and re-release original work as a new pluging. But it seems MIT license should allow it. Anyway if you think it is not correct, unfair or impolite please let me know - create an issue with your thought. I just needed a Grunt task for moving files with the same logic that is in grunt-contrib-copy.
Other move/rename tasks out there:
- grunt-move - it doesn't support glob pattens
- grunt-contrib-rename (despite its name it's not an official plugin from Grunt Team - bad naming) - it doesn't support glob pattens
Run this task with the grunt copy
command.
Task copies files and folder.
Task targets, files and options may be specified according to the grunt Configuring tasks guide.
Type: Function(content, srcpath)
This option is passed to grunt.file.copy
as an advanced way to control the file contents that are copied.
processContent
has been renamed to process
and the option name will be removed in the future.
Type: String
This option is passed to grunt.file.copy
as an advanced way to control which file contents are processed.
processContentExclude
has been renamed to noProcess
and the option name will be removed in the future.
Type: String
Default: grunt.file.defaultEncoding
The file encoding to copy files with.
Type: Boolean
or String
Default: false
Whether to copy or set the destination file and directory permissions.
Set to true
to copy the existing file and directories permissions.
Or set to the mode, i.e.: 0644
, that copied files will be set to.
Type: Boolean
Default: false
Whether to preserve the timestamp attributes(atime
and mtime
) when copying files. Set to true
to preserve files timestamp. But timestamp will not be preserved when the file contents or name are changed during copying.
Type: Boolean
Default: false
Normally copy
task will fail to overwrite readonly files. This option make it to overwrite existing readonly files.
Type: Boolean
Default: false
If specified then existing files will not be overwritten (by default copy
task overwrites existing files).
Type: Boolean
Default: false
If specified then copied files will be deleted (i.e. moved instead of copied effectively - see also move
task). Note that skipped to copy files won't be deleted.
copy: {
main: {
files: [
// includes files within path
{expand: true, src: ['path/*'], dest: 'dest/', filter: 'isFile'},
// includes files within path and its sub-directories
{expand: true, src: ['path/**'], dest: 'dest/'},
// makes all src relative to cwd
{expand: true, cwd: 'path/', src: ['**'], dest: 'dest/'},
// flattens results to a single level
{expand: true, flatten: true, src: ['path/**'], dest: 'dest/', filter: 'isFile'},
],
},
},
This task supports all the file mapping format Grunt supports. Please read Globbing patterns and Building the files object dynamically for additional details.
Here are some additional examples, given the following file tree:
$ tree -I node_modules
.
├── Gruntfile.js
└── src
├── a
└── subdir
└── b
2 directories, 3 files
Copy a single file tree:
copy: {
main: {
expand: true,
src: 'src/*',
dest: 'dest/',
},
},
$ grunt copy
Running "copy:main" (copy) task
Created 1 directories, copied 1 files
Done, without errors.
$ tree -I node_modules
.
├── Gruntfile.js
├── dest
│ └── src
│ ├── a
│ └── subdir
└── src
├── a
└── subdir
└── b
5 directories, 4 files
Copying without full path:
copy: {
main: {
expand: true,
cwd: 'src',
src: '**',
dest: 'dest/',
},
},
$ grunt copy
Running "copy:main" (copy) task
Created 2 directories, copied 2 files
Done, without errors.
$ tree -I node_modules
.
├── Gruntfile.js
├── dest
│ ├── a
│ └── subdir
│ └── b
└── src
├── a
└── subdir
└── b
5 directories, 5 files
Flattening the filepath output:
copy: {
main: {
expand: true,
cwd: 'src/',
src: '**',
dest: 'dest/',
flatten: true,
filter: 'isFile',
},
},
$ grunt copy
Running "copy:main" (copy) task
Copied 2 files
Done, without errors.
$ tree -I node_modules
.
├── Gruntfile.js
├── dest
│ ├── a
│ └── b
└── src
├── a
└── subdir
└── b
3 directories, 5 files
Copy and modify a file:
To change the contents of a file as it is copied, set an options.process
function as follows:
copy: {
main: {
src: 'src/a',
dest: 'src/a.bak',
options: {
process: function (content, srcpath) {
return content.replace(/[sad ]/g, '_');
},
},
},
},
Here all occurrences of the letters "s", "a" and "d", as well as all spaces, will be changed to underlines in "a.bak". Of course, you are not limited to just using regex replacements.
To process all files in a directory, the process
function is used in exactly the same way.
NOTE: If process
is not working, be aware it was called processContent
in v0.4.1 and earlier.
By default, if a file or directory is not found it is quietly ignored. If the file should exist, and non-existence generates an error, then add nonull:true
. For instance, this Gruntfile.js entry:
copy: {
main: {
nonull: true,
src: 'not-there',
dest: 'create-me',
},
},
gives this output:
$ grunt copy
Running "copy:main" (copy) task
Warning: Unable to read "not-there" file (Error code: ENOENT). Use --force to continue.
Aborted due to warnings.
Run this task with the grunt move
command.
Task moves files and folder.
Task targets, files and options may be specified according to the grunt Configuring tasks guide.
Type: Boolean
Default: false
Normally move
task will fail to overwrite readonly files. This option make it to overwrite existing readonly files.
Type: Boolean
Default: false
If specified then existing files will not be overwritten (by default move
task overwrites existing files).
- 2018-04-06 v1.0.0 forked grunt-contrib-copy [object Object] Create move task
Task submitted by Sergei Dorogin
This file was generated on Fri Apr 06 2018 22:13:07.