Friday, December 9, 2022

[FIXED] How do I check if a directory exists in a Nushell script?

Issue

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


Solution

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.



Answered By - NotTheDr01ds
Answer Checked By - Senaida (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.