Linux basename command

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

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

This document 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.