Files and folders

The basename command

Prints only the final name of a path, and can strip a suffix such as an extension.

Syntax

basename <ruta> [sufijo]Try it in the terminal →

Most useful options

-a, --multipleTreats each argument as a path. Without it, the second argument is taken as a suffix
-s, --suffix <sufijo>Removes that trailing suffix from each path. It turns on -a by itself
-z, --zeroEnds each output line with a NUL byte instead of a newline. Works well with xargs -0
--helpShows the help and exits
--versionShows the version of the coreutils package it belongs to

Copy-ready examples

$ basename /etc/passwdfinal name of an absolute path.

Example output

passwd
$ basename /home/alumno/informe.txt .txtremoves the extension by passing the suffix as second argument.

Example output

informe
$ basename -s .txt /home/alumno/informe.txtthe same with the -s option, clearer when there are several paths.

Example output

informe
$ basename -a /home/alumno/a.txt /home/alumno/b.txtprocesses several paths at once, with one output per line.

Example output

a.txt
b.txt
$ for f in *.jpg; do basename "$f" .jpg; donein a loop, the name of each image in the folder without its extension.

Example output

x
y

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
basename: missing operandYou ran basename without any path.Pass the path, for example basename /path/to/file.txt. To see the help, run basename --help.
basename: extra operand 'c'You passed three arguments without -a or -s. Without them, the first is the path, the second the suffix, and the third is extra.For several paths, use -a: basename -a a b c.

Typical mistakes

  • Without a suffix it removes nothing: basename informe.txt returns informe.txt. To strip the extension, pass it as second argument or use -s.
  • With several paths and no -a, the second argument is taken as a suffix: basename a/x b/y prints x and gives no warning.
  • It does not look at the disk: basename /noexiste/nada returns nada without warning. If you need to know whether it exists, check with test -e.
  • A trailing slash is ignored: basename /home/alumno/ returns alumno, not an empty string.

Differences from similar commands

  • dirname: returns the folder, the opposite of basename: it removes the last segment instead of keeping it.
  • realpath: gives the full absolute path, resolving links. basename only trims text and does not check whether the path exists.

FAQ

How do I split a file's folder and name at the same time?

Use dirname and basename together: dirname gives the folder and basename the name. That way you do not have to cut the path by hand with cut or sed.

What happens if the name has spaces?

Quote the variable: basename "$ruta". Without quotes, the shell splits the path into several arguments and basename fails on the extra ones.

Can I use it to rename files?

No: basename only works out the name and changes nothing. To rename, use mv, and pass it the result of basename stored in a variable.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course