dirname

display directory components of path name 

Command, KornShell Built-in


SYNOPSIS

dirname [pathname]


DESCRIPTION

dirname strips off the trailing part of a file name. The result is the path name of the directory that contains the file. This is useful in shell scripts.

Note:

dirname makes no attempt to validate the path name; for validation, use the pathchk command.

dirname follows these rules:

Note:

On Windows systems, these rules handle forward slashes in the same way as backslashes.

dirname returns device names as you type them in pathname -- if you include a slash, dirname returns the device name with the following slash; if you don't include a slash, the device name is returned with no following slash.


EXAMPLES

The command

dirname src/lib/printf.c

produces

src/lib

DIAGNOSTICS

Possible exit status values are:

0 

Successful completion.

>0 

An error occurred.


NOTE

dirname is provided as both an external utility and a built-in MKS KornShell utility.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022. Windows Server 2025.

On Windows systems, dirname removes the final backslash or slash and all following characters. On UNIX and POSIX-compliant systems, backslash is a valid file name character and is not stripped.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Commands:
basename, pathchk, sh


PTC MKS Toolkit 10.5 Documentation Build 40.