| 1 |
--- |
| 2 |
title: Basic usage |
| 3 |
description: Use the command-line interface (CLI) to perform basic tasks. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
weight: 20 |
| 7 |
aliases: [/overview/usage/,/extras/livereload/,/doc/usage/,/usage/] |
| 8 |
--- |
| 9 |
|
| 10 |
## Test your installation |
| 11 |
|
| 12 |
After [installing][] Hugo, test your installation by running: |
| 13 |
|
| 14 |
```sh |
| 15 |
hugo version |
| 16 |
``` |
| 17 |
|
| 18 |
## Display available commands |
| 19 |
|
| 20 |
To see a list of the available commands and flags: |
| 21 |
|
| 22 |
```sh |
| 23 |
hugo help |
| 24 |
``` |
| 25 |
|
| 26 |
To get help with a subcommand, use the `--help` flag. For example: |
| 27 |
|
| 28 |
```sh |
| 29 |
hugo server --help |
| 30 |
``` |
| 31 |
|
| 32 |
## Build your project |
| 33 |
|
| 34 |
To build your project, `cd` into your project directory and run: |
| 35 |
|
| 36 |
```sh |
| 37 |
hugo build |
| 38 |
``` |
| 39 |
|
| 40 |
The [`hugo build`][] command builds your project, publishing the files to the `public` directory. To publish your project to a different directory, use the [`--destination`][] flag or set [`publishDir`][] in your project configuration. |
| 41 |
|
| 42 |
> [!NOTE] |
| 43 |
> Hugo does not clear the `public` directory before building your project. Existing files are overwritten, but not deleted. This behavior is intentional to prevent the inadvertent removal of files that you may have added to the `public` directory after the build. |
| 44 |
> |
| 45 |
> Depending on your needs, you may wish to manually clear the contents of the `public` directory before every build. |
| 46 |
|
| 47 |
## Draft, future, and expired content |
| 48 |
|
| 49 |
Hugo allows you to set `draft`, `date`, `publishDate`, and `expiryDate` in the [front matter][] of your content. By default, Hugo will not publish content when: |
| 50 |
|
| 51 |
- The `draft` value is `true` |
| 52 |
- The `date` is in the future |
| 53 |
- The `publishDate` is in the future |
| 54 |
- The `expiryDate` is in the past |
| 55 |
|
| 56 |
> [!NOTE] |
| 57 |
> Hugo publishes descendants of draft, future, and expired [branch](g) pages. To prevent publication of these descendants, use the [`cascade`][] front matter field to cascade [build options][] to the descendant pages. |
| 58 |
|
| 59 |
You can override the default behavior when running `hugo build` or `hugo server` with command line flags: |
| 60 |
|
| 61 |
```sh |
| 62 |
hugo build --buildDrafts # or -D |
| 63 |
hugo build --buildExpired # or -E |
| 64 |
hugo build --buildFuture # or -F |
| 65 |
``` |
| 66 |
|
| 67 |
Although you can also set these values in your project configuration, it can lead to unwanted results unless all content authors are aware of, and understand, the settings. |
| 68 |
|
| 69 |
> [!NOTE] |
| 70 |
> As noted above, Hugo does not clear the `public` directory before building your project. Depending on the _current_ evaluation of the four conditions above, after the build your `public` directory may contain extraneous files from a previous build. |
| 71 |
> |
| 72 |
> A common practice is to manually clear the contents of the `public` directory before each build to remove draft, expired, and future content. |
| 73 |
|
| 74 |
## Develop and test your site |
| 75 |
|
| 76 |
To view your site while developing layouts or creating content, `cd` into your project directory and run: |
| 77 |
|
| 78 |
```sh |
| 79 |
hugo server |
| 80 |
``` |
| 81 |
|
| 82 |
The [`hugo server`][] command builds your site and serves your pages using a minimal HTTP server. When you run `hugo server` it will display the URL of your local site: |
| 83 |
|
| 84 |
```text |
| 85 |
Web Server is available at http://localhost:1313/ |
| 86 |
``` |
| 87 |
|
| 88 |
While the server is running, it watches your project directory for changes to assets, configuration, content, data, layouts, translations, and static files. When it detects a change, the server rebuilds your site and refreshes your browser using [LiveReload][]. |
| 89 |
|
| 90 |
Most Hugo builds are so fast that you may not notice the change unless you are looking directly at your browser. |
| 91 |
|
| 92 |
### LiveReload |
| 93 |
|
| 94 |
While the server is running, Hugo injects JavaScript into the generated HTML pages. The LiveReload script creates a connection from the browser to the server via web sockets. You do not need to install any software or browser plugins, nor is any configuration required. |
| 95 |
|
| 96 |
### Automatic redirection |
| 97 |
|
| 98 |
When editing content, if you want your browser to automatically redirect to the page you last modified, run: |
| 99 |
|
| 100 |
```sh |
| 101 |
hugo server --navigateToChanged |
| 102 |
``` |
| 103 |
|
| 104 |
## Deploy your site |
| 105 |
|
| 106 |
> [!NOTE] |
| 107 |
> As noted above, Hugo does not clear the `public` directory before building your project. Manually clear the contents of the `public` directory before each build to remove draft, expired, and future content. |
| 108 |
|
| 109 |
When you are ready to deploy your site, run: |
| 110 |
|
| 111 |
```sh |
| 112 |
hugo |
| 113 |
``` |
| 114 |
|
| 115 |
This builds your site, publishing the files to the `public` directory. The directory structure will look something like this: |
| 116 |
|
| 117 |
```tree |
| 118 |
public/ |
| 119 |
├── categories/ |
| 120 |
│ ├── index.html |
| 121 |
│ └── index.xml <-- RSS feed for this section |
| 122 |
├── posts/ |
| 123 |
│ ├── my-first-post/ |
| 124 |
│ │ └── index.html |
| 125 |
│ ├── index.html |
| 126 |
│ └── index.xml <-- RSS feed for this section |
| 127 |
├── tags/ |
| 128 |
│ ├── index.html |
| 129 |
│ └── index.xml <-- RSS feed for this section |
| 130 |
├── index.html |
| 131 |
├── index.xml <-- RSS feed for the site |
| 132 |
└── sitemap.xml |
| 133 |
``` |
| 134 |
|
| 135 |
In a simple hosting environment, where you typically `ftp`, `rsync`, or `scp` your files to the root of a virtual host, the contents of the `public` directory are all that you need. |
| 136 |
|
| 137 |
Most of our users deploy their sites to a [CI/CD](g) platform, where a push[^1] to their remote Git repository triggers a build and deployment. Learn more in the [host and deploy][] section. |
| 138 |
|
| 139 |
[^1]: The Git repository contains the entire project directory, typically excluding the `public` directory because the site is built _after_ the push. |
| 140 |
|
| 141 |
[LiveReload]: https://github.com/livereload/livereload-js |
| 142 |
[`--destination`]: /commands/hugo/#options |
| 143 |
[`cascade`]: /content-management/front-matter/#cascade |
| 144 |
[`hugo build`]: /commands/hugo/ |
| 145 |
[`hugo server`]: /commands/hugo_server/ |
| 146 |
[`publishDir`]: /configuration/all/#publishdir |
| 147 |
[build options]: /content-management/build-options/ |
| 148 |
[front matter]: /content-management/front-matter/ |
| 149 |
[host and deploy]: /host-and-deploy/ |
| 150 |
[installing]: /installation/ |