Bash Programming

How to Remove the File Extension From the Filename in Bash?

“While doing bash programming, you might come across a situation in which you are dealing with files; however, you are only interested in their names without their extensions. In such situations, you need to have some methods through which you can remove the file extension from a filename. Therefore, we have designed this tutorial to show you how you can do this in Linux Mint 21.”

Methods of Removing the File Extension From the Filename in Bash

Removing the extension of a file from its name is very easy in Bash, and you can follow either of the two methods explained below to do this:

Method # 1: Specifying the Extension to be Removed With the Basename Command

To use this method, you simply need to execute the following command:

$ basename /home/system/result.txt .txt

Here, you need to specify the complete path of the file (whose extension you wish to remove), including its filename and extension, followed by the “.txt” extension again. This will tell the basename command to remove this extension from the output. If you have a file other than the text file, then you can specify its particular extension instead of “.txt,” and it will be removed in the very same manner.

The name of our file with its extension removed is shown in the image below:

Method # 2: Specifying the Extension to be Removed Along With the “-s” Flag With the Basename Command

To adopt this method, you need to run the following command:

$ basename –s .txt /home/system/result.txt

Again, you can replace the “.txt” extension after the “-s” flag in this command with the particular extension of your file that you wish to remove.

The output of this command is given in the image below:

Conclusion

In this article, we have shared two different ways of removing the file extension from the filename in Linux Mint 21. Now, it is totally a matter of your choice which method you make use of to achieve this goal.

 

Similar Posts