dirname

Linux · Files · reference only

What it does

Returns only the directory part of a path, removing the last component.

Syntax

dirname [opciones] ruta...

Options and parameters

  • ruta

    One or more paths; each is processed separately.

  • -z

    Ends each output line with a null character instead of a newline.

  • --help

    Shows help and exits.

  • --version

    Shows the version and exits.

Usage examples

  • dirname /etc/passwd

    Returns /etc.

  • dirname /home/ana/foto.jpg

    Returns /home/ana.

  • dirname archivo.txt

    Returns a dot (.), the current directory, because the path has no directory.

  • cd "$(dirname "$0")"

    In a script, changes to the directory where the script itself lives.

Notes and common mistakes

  • Like basename, it only works with text and does not check that the path exists.

Related commands