basename Command in Linux | Explained

The basename command utility is used to extract the last name of the directory from the path and removes the characters from the suffix of the path. The significance of the basename command is to utilize it where the file name needs to be removed from the line.

In this write-up, different examples demonstrate the usage of the basename command utility in Linux with the following outline:

Let’s get into the basics of basename command.

What is the basename Command in Linux?

Print the leading directory name in Linux by removing other components from the path and trailing characters from the suffix. The command’s general syntax is:

$ basename [Name] [Suffix]

In this general syntax, the directory’s path is provided, after which the command utility will extract the directory name only from the provided path.

Another way of using the basename command is by following the general syntax:

$ basename [Options] [Name]

In this general syntax, different options can be used with the basename command, and the explanation of the options are:

OptionsExplanation
-aIt can support the multiple arguments in the same command
-sIt is used to remove the trailing suffix
-zIt is used to end the line with the null value
–helpIt will display the help menu of the basename command utility
–versionIt will display the installed version of the basename command utility

The understanding of the options mentioned above with the basename command utility has been explained in the next section.

How to Use the basename Command Utility in Linux?

We have already discussed two different syntaxes for using the basename command, which will be explained with some examples.

Example 1: Extracting Single FileName

For example, we have a file in the Downloads directory whose path is “/home/itslinux/Downloads/Alex.txt/”, so the filename “Alex.txt” can be extracted using the basename command:

$ basename /home/itslinux/Downloads/Alex.txt/

In the above output, it can be seen that the basename of the file has been extracted from its entire path and also the characters “/” in the suffix has been removed.

Example 2: Extract Names and Remove Trailing Suffix

To remove the trailing suffix from the base file name, use the “-s” option of the basename command utility. For example, in the above example, we wanted to display the “Alex” without its extension, then run the command:

$ basename -s .txt /home/itslinux/Downloads/Alex.txt

Example 3: Extract Multiple Filenames

To use the multiple paths and extract multiple names, use the “-a” option of the basename command utility:

$ basename -a /home/itslinux/Downloads/Alex.txt/ /usr/bin/clear

Example 2: Extract the File Name in the Bash Script

We will create a bash script and extract the file name from the file’s path. Use the nano text editor to open the file having the basename_file with the “sh” extension:

$ nano basename_file.sh

Write the script in which we save the file with its complete path in variable “X” and then use the basename command to extract the file name only:

X="/home/itslinux/code.deb" basename "$X"

Run the bash script after saving the file:

$ bash basename_file.sh

Similarly, the basename can be applied to various similar scenarios elaborated above.

Conclusion

In Linux, the basename command utility extracts the base file’s name by removing its entire path, including the suffix characters. Like other commands, basename is also said to be a significant utility. This post has briefly demonstrated the working/usage of the basename command in Linux.