Linux basename command

Updated: 03/13/2021 by Computer Hope
basename command

On Unix-like operating systems, the basename command strips directory information and suffixes from file names.

This page covers the GNU/Linux version of basename.

Description

basename prints file name NAME with any leading directory components removed. It can optionally also remove any trailing suffix.

Syntax

basename NAME [SUFFIX]
basename OPTION... NAME...

Options

-a, --multiple Support multiple arguments and treat each as a NAME.
-s, --suffix=SUFFIX Remove a trailing suffix SUFFIX, such as a file extension.
-z, --zero Separate output with NUL rather than a newline.
--help Display help information and exit.
--version Output version information and exit.

Examples

basename /usr/bin/sort

Outputs the string "sort".

basename include/stdio.h .h

Outputs the string "stdio".

basename -s .h include/stdio.h

Outputs the string "stdio".

basename -a any/str1 any/str2

Outputs the string "str1" followed by the string "str2".

expr — Evaluate arguments as an expression.