basename

Linux · Files · reference only

What it does

Strips the directory from a path and leaves only the file name. It can also strip a suffix, such as the extension.

Syntax

basename ruta [sufijo]
basename [opciones] ruta...

Options and parameters

  • -a

    Accepts several paths at once and handles each separately.

  • -s sufijo

    Strips that suffix from the name; implies -a.

  • -z

    Ends each output line with a null character instead of a newline.

  • --help

    Shows help and exits.

  • --version

    Shows the version and exits.

Usage examples

  • basename /etc/passwd

    Returns passwd, without the directory.

  • basename /home/ana/foto.jpg .jpg

    Returns foto: strips the directory and the extension.

  • basename -a /var/log/syslog /var/log/auth.log

    Returns the name of each path, one per line.

  • basename -s .sh /usr/local/bin/backup.sh

    Returns backup, without the .sh extension.

Notes and common mistakes

  • It does not check that the path exists: it only manipulates text.
  • It is very useful in scripts to build output names from input names.

Related commands