Documentationcurrent version
Help us improve the docs by fixing typos and proposing enhancements.


Action "lxc.file.exists"

Check if the file exists in a container.


const {$status} = await nikita.lxc.file.exists({
  container: 'my_container',
  target: '/root/a_file'
})`File exists: ${$status}`)


  • Push recursive directories
  • Handle unmatched target permissions
  • Handle unmatched target ownerships
  • Detect name from lxd_target

Implementation change

Previous implementation used lxc.query action to retrieve the content of the file and then determine if it exists or not:

lxc query --request GET /1.0/instances/container_name/files?path=file_path

It presents two problems:

  1. The file is fetch which introduce delay and be unacceptable for large file.
  2. The current LXD version throw an error when the file is empty, see LXD issue #11388.

The LXD API exposes a REST endpoint to obtain file metadata. However, lxc query don't support the HEAD HTTP method, see LXD issue #11383.

This implementation uses the lxc.exec action to run the existence file test directly inside the container.

Schema definitions

definitions =
    type: 'object'
        $ref: 'module://@nikitajs/lxd/src/init#/definitions/config/properties/container'
        type: 'string'
        description: '''
        File location in the form of "<path>".
    required: ['container']


handler = ({config}) ->
  {$status} = await @lxc.exec
    $header: "Check if file exists in container #{config.container}"
    container: config.container
    command: "test -f #{}"
    code: [0, 1]
  exists: $status


module.exports =
  handler: handler
    definitions: definitions
    shy: true
Edit on GitHub

Nikita is an open source project hosted on GitHub and developed by Adaltas.