How to Use Basename Command in Linux

The basename command in Linux is a part of the GNU Coreutils. It is often utilized for bash scripting projects, especially in parameter substitution for automated projects. Although this tool doesn’t have a lot of general uses, basename is a nifty tool to learn when working in a UNIX command line.

What is the Basename Command in Linux?

The basename command removes the directory and trailing suffixes from a pathname and prints the filename as an output.

It works by reading the user-inputted string and removing every component except characters that appear after the last slash. By default, the command will print the filename with its extension.

Here are some short examples to show how basename operates:

example of basename command in Linux

The Basename command in Linux is “naive” because it will process any user input as long as it’s a string. When processing an invalid query, the command will automatically print the first word regardless of whether it’s processing a pathname or not.

Unknown strings with three or more words will not go through because the third word is considered an extra parameter.

What is the Purpose of Basename Command?

Casual users will likely not encounter basename, especially if they use browsers for their daily tasks.  However, this tool is very important in developing highly-functional bash scripts that have automation capabilities.

Specifically, basename is used when a script needs to run another script to finish its operation. Script makers use basename to extract the filename and plug it into the echo command to show the overall process log.

You can just deploy the other scripts without basename, and it should still work. However, diagnosing and debugging problems would be a pain in the neck for a task. It also helps a lot when confirming the overall script functionality.

How to Use Basename in Linux?

Basename’s capabilities expand based on the additional argument added to each command. You can customize your code to only remove pathname, or remove file extension as well.

It’s up to the users to choose which option is optimal based on the situation. In basic scripts, the default option is enough to do the job.

Here’s how to use basename by default to remove the pathname only. Type the basename command and follow it up with the pathname. The filename will appear with its file extension regardless of what it is.

$ basename /home/grumbot/Documents/grian.exe
example of basename command in Linux

Notice the additional operand or “argument” before the pathname? The -s argument requires the basename to remove any file extensions or suffixes. Let’s learn more about these arguments below.

Basename Arguments

There are a total of five arguments that basename can utilize for customization. It is not as versatile compared to other scripting commands. In fact, the last three arguments are only for getting more information about the command. However, this actually helps a lot in streamlining the overall scripting process.

Obtaining NAME for Multiple Arguments (-a, –multiple)

Yes, you can add more pathnames in just one input of the basename command, but you need to add the -a or --multiple before the pathname.

In the example below, all the files existing in the Documents folder are extracted and printed in the command as an output.

~$ basename -a /home/grumbot/Documents/grian.exe scar.exe impulse.exe pearl.exe
basename -a option

Note: The basename command doesn’t have the capability to check if all these files exist within the folder, or if they exist at all. Basename only reads what’s on the string and will extract all the characters after the last slash.

You can put two pathnames with different files on the end, which will give the same results. But that would be a more tedious method and a more cluttered command line.

~$ basename -a /home/grumbot/Documents/grian.exe /home/grumbot/Documents/scar.exe
basename -a for multiple pathnames

Removing File Extension from NAMES (-s, –suffix)

Another argument that you can use is -s or -suffix operand, which removes the extension from the file name. When using this argument, the -a argument is already implied. The user doesn’t have to add that to the command to input more entries.

Continuing on the examples from above, only the filename would be printed as an output if you add the suffix argument.

Type -s after the command, add the file extension you want to remove and then your pathname.

$ basename -s .exe /home/grumbot/Documents/grian.exe scar.exe impulse.exe pearl.exe
basename command in linux -s option

Adding NUL Separator to the Output (-z, –zero)

The next operand is the -z or --zero, which only adds a NUL character at the end of the output instead of the traditional newline. Type -z before the pathname to activate this argument.

$ basename -z /home/grumbot/Documents/grian.exe
basename command -z

Basename Cheatsheet (–help)

You can type basename --help to see the list of available arguments just in case you forgot them or you want to see the default examples.

all basename options

Getting Additional Information (–version)

And lastly, type baseline --version to see the latest updates, licenses, and other details if you’re ever curious about these things.

Can You Get the Filename Without Using Bashname?

You can assign a variable and give it the pathname as the value. In this example, we assign the variable g with grian.txt.

$ g=grian.txt

Once the value is assigned, you can use echo to display the filename (with extension).

$ echo "${g##*/}”
basename alternative

However, this method might not be a good option due to the extra step of assigning value to a variable.

Looking for something else? You might be looking for the dirname command, which cuts down the last slash from a pathname. This command is a go-to tool if the user wants to obtain a file’s complete directory tree, which might be useful in some situations.  

Overall, basename is a simple command that makes scripting and debugging a lot easier. It also allows users to get a quick glimpse of the overall program roadmap.

I hope this guide clears out basename’s functions and usages. Check out other guides to improve your Linux command-line experience!

If this guide helped you, please share it. ?

Leave a Reply
Related Posts