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
-aAccepts several paths at once and handles each separately.
-s sufijoStrips that suffix from the name; implies -a.
-zEnds each output line with a null character instead of a newline.
--helpShows help and exits.
--versionShows the version and exits.
Usage examples
basename /etc/passwdReturns passwd, without the directory.
basename /home/ana/foto.jpg .jpgReturns foto: strips the directory and the extension.
basename -a /var/log/syslog /var/log/auth.logReturns the name of each path, one per line.
basename -s .sh /usr/local/bin/backup.shReturns 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.