6

I would like to know how I check if a directory exists in Nushell?

stax76
  • 416
  • 4
  • 12

1 Answers1

8

Use the path exists builtin. Examples:

> "/home" | path exists
true
> "MRE" | path exists
false
> "./nu-0.71.0-x86_64-unknown-linux-gnu" | path exists
true
> [ '.', '/home', 'MRE'] | path exists
╭───┬───────╮
│ 0 │ true  │
│ 1 │ true  │
│ 2 │ false │
╰───┴───────╯
> if ([ '.', '/home', '/proc'] | path exists | reduce -f true { |it, acc| $acc and $it }) {
    "All directories exists"
} else {
    "One ore more directories are missing"
}
All directories exists

See help path exists for more details and help path for more path helper builtins.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70