Get Filename without Extension in Bash

Get Filename without extension in Bash

TL;DR
If you know extension of the file, you can use basename command and pass filename and extension as parameters to retrieve filename without extension in bash. This is useful when you have file extension like .tar.gz

Using basename Command

Use the basename command to get the filename without extension in Bash.

First, we saved the file’s name with its complete path in a variable named filename. Then, we used the basename command to retrieve the filename from the specified path while "${filename%.*}" was the parameter expansion, used to remove the file extension from the given path. How?

In "${filename%.*}", the % operator eliminated the shortest matching suffix from the given variable value, so ${filename%.*} removed the shortest string matching the pattern .* which represented the file extension from the end of the filename variable. The final value (/home/john/Desktop/file) that we will get will be passed to the basename command to extract the file’s name only.

If you want to retrieve the file name without extension but with a complete path, then the following solutions are for you.

Using cut Command

Use the cut command to get the filename without extension in Bash.

Here we used the echo command to print the filename variable’s value. This value was then forwarded to the cut command using the pipeline represented with the | symbol; we used the cut command to extract the base file name from the specified path.

We used the -f option to mention the field(s) to retrieve/extract, and -d was used to specify the delimiter. In the above example, we used a dot (.) as the delimiter, a file extension separator. Here, we used the -f 1 to extract the first field before the delimiter and printed it on the console, as demonstrated in the above output.

Using awk Command

Use the awk command to get the filename without extension in Bash.

This example code is similar to the previous example, but we used the awk command to retrieve the base filename without extension. Here, -F was used to specify the field separator, which was . and {print $1} means to print the first field before the delimiter.

Using sed Command

Use the sed command to get the filename without extension in Bash.

This code snippet is similar to the last two examples, but we used the sed command with regular expression. Unlike the previous example, we used the echo command to print the filename variable’s value; this value was then piped to the sed command. We used the sed command to extract the base filename from the specified path. In sed, we used different things to do various tasks that are briefly described below.

The s was used to search and replace operations on the input text. The \.[^.]*$ regex pattern matched the file extension at the end of a string. Here, backslash (\) was used to escape the dot/period (.), so it can only match the literal period character instead of matching any other character. The [^.]* matched any character that wasn’t a period, which is a file extension and finally, $ was used to match the end of a string.

Note that the replacement string in the sed is empty to remove the file extension from the input text.

That’s all about how to get filename without extension in Bash.

Was this post helpful?

Leave a Reply

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