Using basename Command In Linux (Examples)

basename command in Linux is used as a way to strip directory details and suffix from file names. Means, it will actually print the file name NAME with any directory components removed. To use this command, the user needs just to write ‘basename’ followed by desired file name or filename with full directory path.

Syntax to use basename command in Linux:

basename NAME [SUFFIX]
or
basename OPTION NAME

Explanation: In the above syntax, the NAME stands for the file name for file name with its path, while the OPTION indicates to various available options that can be used with this command. However, if the user want to strip off the suffix of a file, they can use SUFFIX at the end of file name.

Examples to use basename command in Linux

basename /var/www/servonode
servonode

In this example, the basename command removes the directories from the specified path for a file servonode.

Basename -a /var/www/servonode /etc/nginx/default
servonode
default

In this example, we have used multiple inputs to try basename command, so we used -a (–multiple) option. So, the multiple separate results are shown.

basename /var/www/servonode.txt .txt
servonode

In this case, the basename command is used to strip file extension .txt along with removing all trailings as well. So, the result is shown just servonode.

Available options to use with basename command in Linux

Also Read:   Using cfdisk Command In Linux- Examples

-a | –multiple: With this option, you can input multiple arguments and the command will treat each as a NAME.
-s | –sufffix=SUFFIX: Using this option, the command removes a trailing suffix SUFFIX as discussed in above example.
-z: This option separates the output with NULL, than a newline.
–help: Displays help information and exits
–version: Displays the version details and exits

Nishant

Nishant Verma is a senior web developer who love to share his knowledge about Linux, SysAdmin, and more other web handlers. Currently, he loves to write as content contributor for ServoNode.