NAME
basename, dirname
— separate path
components
SYNOPSIS
basename |
[-z] path
[suffix] |
basename |
[-z] -a
path… |
basename |
[-z] -s
suffix path… |
dirname |
[-z] path… |
DESCRIPTION
Write the final or pre-final segments of paths, stripping suffix, if matched, to the standard output stream, separated by newlines.
Trailing slashes are removed; basename
then removes everything up to the final slash, and
dirname removes everything following it, then strips
trailing ones. If a path doesn't contain slashes,
basename outputs it verbatim, and
dirname outputs
".".
These are strictly string operations, and do not depend on the filesystem state.
OPTIONS
-a,--multiple- Accept multiple paths.
-s,--suffix=suffix- Accept multiple paths and set suffix.
-z,--zero- Separate output lines with null bytes instead of newlines.
EXAMPLES
$basename/bin/sort sort$basenameinclude/stdio.h .h stdio$basename-s.h include/stdio.h /usr/include/stdlib.h stdio stdlib$basename-adir/file1 dir/dir2/file2 file3 file1 file2 file3$dirname/usr/local/bin/ /usr/local$dirnamedir1/str dir2/str dir1 dir2$dirnamestdio.h .
STANDARDS
Conforms to IEEE Std 1003.1-2024
(“POSIX.1”), with no special processing for
//. -zas and multiple
paths are extensions, originating from the GNU system,
also present in FreeBSD.
HISTORY
basename appeared, fully formed, save for
lacking a newline at the end, in Version 7 AT&T
UNIX, as
basename(1) – "strip filename affixes":
CB-UNIX at or before 2.1 contains unrelated shell implementations (newline included) of both. CB-UNIX was, among others, the basis for AT&T System III UNIX, where it first saw light outside AT&T.
These implementations continued to evolve, growing peculiar preservational handling of the // prefix, until AT&T System V Release 3 UNIX, which IEEE Std 1003.2-1992 (“POSIX.2”) standardised, requiring that handling on systems where //-prefixed paths are different from /-prefixed ones. 4.3BSD-Reno includes both, without //.