Linux dirname command

Updated: 05/04/2019 by Computer Hope
dirname command

On Unix-like operating systems, the dirname command strips the last part of given file name, outputting the complete pathname of the directory where the file is located.

This page covers the GNU/Linux version of dirname.

Syntax

dirname [OPTION] NAME...

Description

dirname outputs each NAME with its last non-slash component and trailing slashes removed; if NAME contains no /'s, dirname outputs a single '.' (meaning the current directory).

-z, --zero Separate output with NUL rather than a newline.
--help Display a help message and exit.
--version Display version information and exit.

Examples

dirname /usr/bin

Returns:

/usr
dirname stdio.h

Returns:

.

basename — Strip directory information and suffixes from file names.
expr — Evaluate arguments as an expression.