How to Use the Basename Command in Linux

Wondering how to use the basename command in Linux?

$basename is a Linux command that is used to remove the directory portion and the suffix portion of the file path. On execution, it only prints the last part of the path. This command is useful when dealing with long paths and we only need to access the file at the end of it. 

If you want to learn how to use the basename command in Linux, then you are at the right place as this article will be guiding you through all the different ways $basename is used.

So, hang on tight for an informative ride!

The $basename Command.

$basename can be used in two different ways and there is a different syntax for each action. To remove the trailing directories, we simply use the following syntax:

$basename <file path>

To remove the suffix following the file name, we can use the following syntax:

$basename <file path> <suffix name>

These are the two available syntaxes. The use of these syntaxes is covered in the example given below.

Removing Directories Using $basename Command.

To obtain just the filename from a long file path, follow these steps.

  1. Open the Command Terminal on your Linux system. 

We will be using paths and file names present in our system. Using this as an example you can attempt to use the $basename command on your file paths. 

  1. Once opened, type in the following command:
$ basename host/home/example/wire

You should see an output similar to the image below.


Let’s take a look at another example. 

$ basename host/home/snap/snap-store

You can see in the output that the command removes all the ‘/‘ preceding the file name. 

Basename Command in Linux

You can also use this command to extract file names from multiple paths. For this, you need to use -a with $basename and type in the paths simultaneously one after the other. For example:

$ basename -a host/home/doc host/home/doc1 

An output similar to this should appear on your screen.

Basename Command in Linux


$ basename -a host/home/snap/snap-store host/home/example/wire
Basename Command in Linux

These were the examples in which we got the filename(s) using single and multiple paths. Now, we will look at how we can remove suffixes from file names. 

Removing Suffixes Using $basename Command.

Suffixes following the file name are usually the file extensions. These extensions describe the type of file. For example, in “Helloworld.txt”, “.txt” is the extension and the suffix. We can also use $basename to separate “Helloworld” to get just the word “Hello”. In this case, “world” will become the suffix. 

Follow these steps to remove suffixes from your preferred file names. 

  1. Start by opening the Command Terminal.
  2. Once opened, you can use the following command as an example and attempt at removing your suffix. 
$ basename host/home/example/wire.txt .txt

You should see that the extension is removed. 

Basename Command in Linux
  1. Similarly, we can remove multiple suffixes from multiple file names. For this, we will use -a and -s with $basename and type in the paths one after the other. 
$ basename -a -s .txt host/home/doc host/home/doc1

A similar output should be displayed on your screen. 

Basename Command in Linux
  1. Let’s bisect the name and get the ‘basename‘. 
$ basename host/home/snap/snap-store -snap

This should be seen as the output. 

Basename Command in Linux

These were examples of removing suffixes from file names and paths. With this, you now know how to use the basename command on Linux.

Significance of the Basename Command. 

There are various utility commands that are used by Linux users. $basename command is one of them. 

Admittedly, the utility coming out of $basename might seem minimal, but when you are dealing with thousands and thousands of lines of code and file paths, commands such as $basename become very important. 

Basename helps users by eliminating the need of repeating the same lines of code over and over. A good program is one that performs its intended function and is optimized for the best system performance. 

Paths, directories, and file types are all managed by complex file-managing algorithms known as filesystems. These filesystems are responsible for the efficient reading and writing of files that are available on permanent storage devices.

ZFS as an Alternative

Although the default filesystem for Linux is enough, an alternative is ZFS. One of the key features of ZFS is file compression. Learning how to enable this feature can be of use to you as it will help save space on your storage device.

This article was a guide on how to use the basename command in Linux. We hope that we were able to help you out in understanding the command and its functionality. If we helped you out, please let us know in the comment section down below.  

If this guide helped you, please share it. 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *