Back to the command reference

basename

FilesFundamentals In simulator In real Linux

Strip the directory part from a path

Name: basename: the base name, without the directory

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

basename strips everything up to the last / from a path, leaving the file name, and optionally a suffix too. It works on the string only and doesn't check that the file exists.

Scripts use it to get a file name from a path; in bash, ${path##*/} does the same.

Syntax

basename NAME [SUFFIX]

Common options

SUFFIXAlso remove a trailing suffix
-s / --suffix SUFFIXRemove the suffix SUFFIX (works with -a)
-a / --multipleTake several names, one result per line

Examples

  • $ basename /home/user/projects/hello.py

    Prints hello.py

    Explain
  • $ basename projects/hello.py .py

    Prints hello

    Explain

Related