What is basename?
When you provide a string that contains a filename will full path, basename will remove only the directory portion of it, and return only the filename portion of that string. It can also remove the file extension, and return only the filename without an extension.
3 basename Examples
1. Basename when no file extension
Basename removes the path and displays only the filename. For example, the basename of /etc/passwd is “passwd” as shown below.
$ basename /etc/passwd passwd
2. Basename when there is a file extension
When the filename contains an extension, basename by default will remove the path and return the filename with the extension
$ basename /usr/local/apache2/conf/httpd.conf httpd.conf
3. Get only the filename without extension
If you want to get only the filename with the extension, you should pass the extension as 2nd argument to the basename command as shown below. The following example will return only httpd, as the 2nd argument to the basename command is .conf
$ basename /usr/local/apache2/conf/httpd.conf .conf httpd
Syntax and Options
basename NAME [SUFFIX] basename OPTION
Explain the option of the basename command. e.g. of chmod shown below.
|Short Option||Long Option||Option Description|
|–help||Shows the help page|
|–version||shows the version of basename command|