watch

For filesystem.

Watch for file changes and execute Nu code when they happen.

Command Type

built-in

Signature

> watch {flags} (path) (closure)

Flags

  • --debounce, -d {duration}: Debounce changes for this duration (default: 100ms). Adjust if you find that single writes are reported as multiple events.
  • --glob, -g {string}: Only report changes for files that match this glob pattern (default: all files)
  • --recursive, -r {bool}: Watch all directories under <path> recursively. Will be ignored if <path> is a file (default: true).
  • --quiet, -q: Hide the initial status message (default: false).
  • --verbose, -v: Operate in verbose mode (default: false).

Parameters

  • path: The path to watch. Can be a file or directory.
  • closure: Some Nu code to run whenever a file changes. The closure will be passed operation, path, and new_path (for renames only) arguments in that order (deprecated).

Input/output types:

input output
nothing nothing
nothing table<operation: string, path: oneof<string, nothing>, new_path: oneof<string, nothing>>

Examples

Run cargo test whenever a Rust file changes.

> for _ in (watch . --glob=**/*.rs) { cargo test }

Watch all changes in the current directory.

> watch . | each { print }

Filter, limit and modify watch’s output by using it as part of a pipeline.

> watch /foo/bar
    | where operation == Create
    | first 5
    | each {|e| $"New file!: ($e.path)" }
    | to text
    | save --append changes_in_bar.log

Print file changes with a debounce time of 5 minutes.

> watch /foo/bar --debounce 5min | each {|e| $"Registered ($e.operation) on ($e.path)" | print }

Note: if you are looking to run a command every N units of time, this can be accomplished with a loop and sleep.

> loop { command; sleep duration }

Run cargo test whenever a Rust file changes (with the deprecated closure argument).

> watch . --glob=**/*.rs {|| cargo test }

Notes

When run without a closure, watch returns a stream of events instead.

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.