96

I'm trying to copy all the files in a directory to another directory as part of my build process. It works fine for individual files that I specify explicitly but when I try to copy the whole directory it does weird things like copies the full directory structure (or nothing at all). Here is the relevant part from my GruntFile.js:

copy: {
  myvoice: {
    files: [
      { src:"src/html/index.html", dest:"dist/myvoice/index.html" },
      { src:"src/html/css/style.css", dest:"dist/myvoice/css/style.css" },
      { src:"src/html/js/require.js", dest:"dist/myvoice/js/require.js" },
      { src:"build/myvoice/main.js", dest:"dist/myvoice/js/main.js" },
      { src:"src/html/css/fonts/*", dest:"dist/myvoice/css/fonts/" }
    ]
  }
},

Specifically it's the last line that I can't get to work:

      { src:"src/html/css/fonts/*", dest:"dist/myvoice/css/fonts/" }

5 Answers 5

159

The flatten: true option as in this answer might work for some cases, but it seems to me that the more common requirement (as in my case) is to copy a folder and its sub-folder structure, as-is, to dest. It seems that in most cases if you have sub-folders, they are probably being referenced that way in code. The key to doing this is the cwd option, which will preserve folder structure relative to the specified working directory:

copy: {
  files: {
    cwd: 'path/to/files',  // set working folder / root to copy
    src: '**/*',           // copy all files and subfolders
    dest: 'dist/files',    // destination folder
    expand: true           // required when using cwd
  }
}
4
  • Thanks -- you're right this answer is more what I was looking for when I asked the question. I'd learned to deal the the flattening caused by the previous answer but it was annoying.
    – Evan Hobbs
    Jun 5, 2014 at 15:57
  • 15
    I have lost over an hour for this... If you use cwd options, be sure to turn expand:true. If you don't set expand:true, cwd will not work properly.
    – ducin
    Jul 23, 2014 at 13:13
  • 2
    I had to make sure directory paths end with '/' and add flatten: false to get this working. Aug 31, 2014 at 12:33
  • **/* Thats what I was looking for I was using ** thanks man.
    – Someone
    Mar 18, 2017 at 17:11
46

This task will maintain folder structure if you specify a file glob. What you want is the flatten option which will remove the structure.

{
    expand: true,
    flatten: true,
    src: ['src/html/css/fonts/**'],
    dest: 'dist/myvoice/css/fonts/',
    filter: 'isFile'
}

Find the rest of the available options in the Github repo. Hope this helps.

0
26

I would like to add that changing the format of the glob in src will modify how the copy works.

As pointed out by bmoeskau above, the following will copy everything inside dist/ and move it to path/to/dir (overwriting the destination if it already exists).

copy: {
  files: {
    expand: true,
    dest: 'path/to/dir',
    cwd: 'dist/',
    src: '**'
  }
}

Note however, that:

copy: {
  files: {
    expand: true,
    dest: 'path/to/dir',
    cwd: 'dist/',
    src: '*'
  }
}

Will only copy files inside dist/ as well as directories, but will not copy the contents of those directories to the destination.

Also, the following with src: '*/*' will only copy directories with contents inside dist/. That is, files just inside dist/ will not be copied.

copy: {
  files: {
    expand: true,
    dest: 'path/to/dir',
    cwd: 'dist/',
    src: '*/*'
  }
}

Finally, same as above, but src: '**/**' will copy only files inside dist/ as well as files inside dist/ subdirectories to path/to/dir. So there will be no folders inside the destination.

copy: {
  files: {
    expand: true,
    dest: 'path/to/dir',
    cwd: 'dist/',
    src: '*/*',
    flatten: true,
    filter: 'isFile'
  }
}
5
  • 4
    great explaination! +1
    – myrocode
    Sep 17, 2014 at 9:38
  • 3
    better than the documentation on github, I like examples
    – wukong
    Dec 9, 2014 at 9:16
  • +1 Is there a convention on what the number of stars should mean, for example does ** always mean files and directories, and * just files? Jan 26, 2015 at 9:13
  • 1
    @Imray From the bash manual: two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories. Jan 26, 2015 at 11:06
  • 1
    ** matches everything, whereas **/ only directories and subdirectories (not files). Jan 26, 2015 at 11:09
2

Had to use egdy instead curly braces for the files segment (in Coffeescript)...

copy: {
  files: [
    cwd: 'path/to/files'
    src: '**/*'
    dest: 'dist/files'
    expand: true
  ]
}
0

If you are developing with angular yeoman , then this is the better way to copy with grunt. expand: true is required when using cwd. <%= yeoman.app %> is just the app route ('.').

 {
    expand: true,
     cwd: '<%= yeoman.app %>/data',
     dest: '<%= yeoman.dist %>/data',
     src: ['**']
    }
1
  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations!
    – Blue
    Jul 22, 2016 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.