Excluding directories from build in Hugo

Suppose we have the following Hugo project structure and we want to exclude content/posts/private from getting published because we use this for templating and other purposes that relate to the editing experience:

├── content
│  └── posts
│     ├── private
│     │  └── template.md
│     ├── hello-1.md
│     └── hello-2.md
├── static
├── themes
│  └── typo
└── hugo.toml

If we build this as is using hugo server --buildDrafts, we see the following:

The simplest approach which isn’t favored currently by Hugo, probably because it’s on the path to being deprecated is to add the ignoreFiles property to the root of your hugo.toml file and specify an array of regex patterns of paths you want to be excluded from the build output.

baseURL = '/'
title = 'Blog'
theme = 'typo'

ignoreFiles = ["/content/posts/private"]

...

With this change, you won’t see the traces of the private directory in your public output directory after running hugo and the post listing looks correct:

The more cumbersome and preferred approach is to use mounts. The following needs to be added to hugo.toml:

[module]
  [[module.mounts]]
    source = 'content'
    target = 'content'
  [[module.mounts]]
    source = 'static'
    target = 'static'
  [[module.mounts]]
    source = 'layouts'
    target = 'layouts'

  # ... and more things to add here depending on what and all
  # special Hugo directories you're adding.
  # See https://gohugo.io/hugo-modules/configuration/#default-mounts

Each of those entries are unfortunately mandatory when you use mounts. Once that’s done, we can add the excludeFiles key into the content mount like so:

[module]
  [[module.mounts]]
    source = 'content'
    target = 'content'
    excludeFiles = ['posts/private/*']
  ...

Note that excludeFiles is an array of globs to be matched relative to the source directory and not a list of regex patterns.

The output will be the same as using ignoreFiles.