Basic CLI Features
The command line interface to OpenTofu is the tofu command, which
accepts a variety of subcommands such as tofu init or tofu plan.
To view a list of the commands available in your current OpenTofu version,
run tofu with no additional arguments:
Usage: tofu [global options] <subcommand> [args]
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure
All other commands:
  console       Try OpenTofu expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  force-unlock  Release a stuck lock on the current workspace
  get           Install or upgrade remote OpenTofu modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a OpenTofu resource
  login         Obtain and save credentials for a remote host
  logout        Remove locally-stored credentials for a remote host
  metadata      Metadata related commands
  output        Show output values from your root module
  providers     Show the providers required for this configuration
  refresh       Update the state to match remote systems
  show          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current OpenTofu version
  workspace     Workspace management
Global options (use these before the subcommand, if any):
  -chdir=DIR    Switch to a different working directory before executing the
                given subcommand.
  -help         Show this help output, or the help for a specified subcommand.
  -version      An alias for the "version" subcommand.
(The output from your current OpenTofu version may be different than the above example.)
To get specific help for any specific command, use the -help option with the
relevant subcommand. For example, to see help about the "validate" subcommand
you can run tofu validate -help.
The inline help built in to OpenTofu CLI describes the most important characteristics of each command. For more detailed information, refer to each command's page for details.
Switching working directory with -chdir
The usual way to run OpenTofu is to first switch to the directory containing
the .tf files for your root module (for example, using the cd command), so
that OpenTofu will find those files automatically without any extra arguments.
In some cases though — particularly when wrapping OpenTofu in automation
scripts — it can be convenient to run OpenTofu from a different directory than
the root module directory. To allow that, OpenTofu supports a global option
-chdir=... which you can include before the name of the subcommand you intend
to run:
tofu -chdir=environments/production apply
The chdir option instructs OpenTofu to change its working directory to the
given directory before running the given subcommand. This means that any files
that OpenTofu would normally read or write in the current working directory
will be read or written in the given directory instead.
There are two exceptions where OpenTofu will use the original working directory
even when you specify -chdir=...:
- Settings in the CLI Configuration are not for a specific subcommand and OpenTofu processes them before acting on the - -chdiroption.
- In case you need to use files from the original working directory as part of your configuration, a reference to - path.cwdin the configuration will produce the original working directory instead of the overridden working directory. Use- path.rootto get the root module directory.
Shell Tab-completion
If you use either bash or zsh as your command shell, OpenTofu can provide
tab-completion support for all command names and some command arguments.
To add the necessary commands to your shell profile, run the following command:
tofu -install-autocomplete
After installation, it is necessary to restart your shell or to re-read its profile script before completion will be activated.
To uninstall the completion hook, assuming that it has not been modified manually in the shell profile, run the following command:
tofu -uninstall-autocomplete