Saturday, July 20, 2024
HomeHow toLinux basename Command Tutorial for Beginners (with Examples)

Linux basename Command Tutorial for Beginners (with Examples)

Sometimes, while working on the command line (especially when dealing with shell scripts), you might be interested in only the filename, but what’s available to you is the file’s complete path. So, the requirement is to strip the directory component of the path.

The basename command in Linux is a utility used to extract the base name of a file or directory path, effectively stripping away any leading directory components and optional suffixes. When provided with a full path, basename returns only the file name or the last segment of the path, making it useful in scripting and file manipulation tasks. For example, given the path /home/user/document.txt, basename would return document.txt. Additionally, users can specify a suffix to be removed, such as .txt, leaving just document. This command simplifies handling file names and paths in scripts, enhancing automation and streamlining various file management processes.

This tutorial will explain the basics of ‘basename’ using easy-to-understand examples. But before we jump on to that, it’s worth mentioning that all examples here have been tested on an Ubuntu 24.04 LTS machine.

Linux basename command explained

The basename command allows you to strip off components from filenames that aren’t required. Following is the tool’s syntax:

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

And here’s what the man page says about it:

basename - strip directory and suffix from filenames

Print NAME with any leading directory components removed. If specified, also remove a trailing
SUFFIX.

Following are some Q&A-styled examples that should give you a good idea on how the basename command works.

Q1. How does the basename command work?

By default, if you run the ‘basename’ command with a full path to a file as an input, the command returns the filename in output. For example, when I executed the following command:

basename /home/himanshu/Downloads/analytics.pdf

I got the following output:

analytics.pdf

Q2. Can basename handle multiple inputs?

Yes, it can. But for this to happen, you must use the -a command line option. For example, when I executed the following command:

basename -a /home/himanshu/Downloads/analytics.pdf /home/himanshu/Pictures/test.png

And here’s the output I got:

analytics.pdf
test.png

Q3. How to make basename strip file extension as well?

Sometimes, you might only want to fetch the filename but not its extension. This can be done using the -s command line option (which requires you to pass the suffix as input).

For example, the following command:

basename -s .pdf /home/himanshu/Downloads/analytics.pdf

produced this output:

analytics

Q4. How to make each line output end with NUL?

By default, the newline character is used as a separator in output. However, you can force basename to use NUL as the separator if you want. This can be done using the -z command line option.

For example:

basename -az /home/himanshu/Downloads/analytics.pdf /home/himanshu/Pictures/test.png

And here’s the output:

analytics.pdftest.png

So you don’t see the NUL character here. You need to redirect the output to a file and then open the file in an editor like vim. Here’s what vim showed:

Conclusion

As you can see, basename isn’t a very feature-rich command – its options are limited, and most of those have been discussed here in this tutorial. Once you are done practicing these, head to the tool’s man page to learn more about it.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular