How to Use Basename Command in Linux

Before we begin talking about how to use basename command in Linux, let's briefly understand - What is basename command?

basename is a command-line tool for removing the directory and trailing suffix from file names. It can be particularly useful when you need to manipulate or reference just the filename itself without the directory path.

In this tutorial, you will understand how to use the basename command. We will also address few FAQs on how to use basename command in Linux.

Using the basename Command

There are two syntax formats supported by the basename command:

basename NAME [SUFFIX]
basename OPTION... NAME...

basename takes a filename and prints the filename's last component. It can also remove any trailing suffix if desired. It's a straightforward command with only a few arguments.

Printing the file name with the leading directories is the most basic example:

basename /etc/passwd

The file name will be included in the output:

Output 

passwd

Any trailing / characters are removed with the basename command:

basename /usr/local/
basename /usr/local

The output from both commands will be the same:

Output

local
local

Each output line ends with a newline character by default. Use the -z (--zero) option to terminate the lines with a NUL.

Multiple Inputs

Multiple names can be passed as arguments to the basename command. To do so, use the -a (--multiple) option with the command, followed by a list of files separated by a space.

For example, to get the names of the files /etc/passwd and /etc/shadow, run the following command:

basename -a /etc/passwd /etc/shadow
Output

passwd
shadow

Removing a Trailing Suffix

Pass the suffix as a second parameter to eliminate any trailing suffix from the file name:

basename /etc/hostname name
Output

host

This feature is typically used to remove file extensions:

basename /etc/sysctl.conf .conf
Output

sysctl

Another technique for removing a trailing suffix is to use the -s (--suffix=SUFFIX) option:

basename -s .conf /etc/sysctl.conf
Output

sysctl

You can remove any trailing suffix from numerous names using this syntactic form:

basename -a -s .conf /etc/sysctl.conf /etc/sudo.conf
Output

sysctl
sudo

Example

The following example explains how to use the basename command within a bash for loop to rename all files ending in ".jpeg" in the current directory to ".jpg":

for file in *.jpeg; do
    mv -- "$file" "$(basename $file .jpeg).jpg"
done

If you're using bash as your shell, instead of calling basename, you can use Shell Parameter Expansion to strip the trailing extension.

FAQ's on how to use basename command in Linux

What is the basic syntax of the basename command? 

The basic syntax of the basename command is: basename [OPTION] NAME.

Can the basename command handle multiple filenames at once? 

Yes, the basename command can handle multiple filenames. You can provide multiple pathnames as arguments, separated by spaces.

How can I use basename to extract just the directory name? 

To extract just the directory name from a given pathname, you can use the dirname command instead.

Does basename modify or rename files?

No, basename is a read-only command that only extracts and displays the base name. It does not modify or rename the actual files.

Can I use the basename command in shell scripting and automation tasks? 

Yes, the basename command is frequently used in shell scripting and automation tasks.

Are there any limitations or considerations when using basename?

One limitation to consider is that basename does not support recursive directory processing. It only works on single pathnames or a list of pathnames provided as arguments.

How does the basename command work? 

The basename command extracts the filename or last component of a given pathname. It discards any leading directory path and returns just the base name

Conclusion

The basename command in Linux simplifies file manipulation tasks by extracting and displaying the base name of a given file path, eliminating the directory path and allowing for easier referencing and scripting.

By using the various options available, such as removing file extensions or working with multiple pathnames, you can efficiently manipulate and extract desired information from filenames.

We hope this detailed guide helped you understand how to use the basename command.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.