path dirname

For path.

Get the parent directory of a path.

Command Type

built-in

Signature

> path dirname {flags}

Flags

  • --replace, -r {string}: Return original path with dirname replaced by this string.
  • --num-levels, -n {int}: Number of directories to walk up.

Input/output types:

input output
string string
list<string> list<string>

Examples

Get dirname of a path.

> '/home/joe/code/test.txt' | path dirname
/home/joe/code

Get dirname of a list of paths.

> [ /home/joe/test.txt, /home/doe/test.txt ] | path dirname
╭───┬───────────╮
│ 0 │ /home/joe │
│ 1 │ /home/doe │
╰───┴───────────╯

Walk up two levels.

> '/home/joe/code/test.txt' | path dirname --num-levels 2
/home/joe

Replace the part that would be returned with a custom path.

> '/home/joe/code/test.txt' | path dirname --num-levels 2 --replace /home/viking
/home/viking/code/test.txt

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