Bash Scripting
  • scripting list
  • How to Make Bash Script Executable Using Chmod
  • Shell Script to Check Linux Server Health
  • How to Concatenate String Variables in Bash [Join Strings]
  • How to Do Bash Variable Substitution (Parameter Substitution)
  • Bash Parameter Expansion with Cheat Sheet
  • Bash getopts with Examples
  • How to Pass all Arguments in Bash Scripting
  • Bash Function Return Value
  • Bash Loop Through Lines in a File
  • Bash readarray with Examples
  • Bash let with Examples
  • Bash expr with Examples
  • Bash read password
  • Bash for Loop Range Variable
  • Bash Arrays Explained
  • Ways to Find Bash Array Length
  • Bash Split String by Delimiter
  • Bash if Options
  • Bash If Statements for String Comparison
  • Debugging Techniques for Bash Scripts
  • Determining if a Bash String is Empty
  • Bash if Statement with Multiple Conditions
  • Meaning of Shebang in Bash Scripts
  • How to Comment Code in Bash Script
  • How to Read CSV File in Bash
  • Bash Scripting: How to Check if File Exists
  • Bash If Else Statements: Examples and Syntax
  • Bash Scripting: How to Check if Directory Exists
  • Bash eval Command with Examples
  • How to Use Sleep Command in Bash Scripting
  • Bash Associative Arrays with Examples
  • Bash Script for Counting Lines in a File
  • How to Use While Loop in Bash for Efficient Scripting
  • Bash basename Command with Examples
  • How to Create Multiline Strings in Bash
  • How to Use Bash if With && Operator
  • 50 Bash Script Examples to Kickstart Your Learning
  • Case statement in Bash Shell Scripting
  • Trimming White Space in Bash
  • How to Extract Filename from Absolute Path in Bash
  • How to Get Directory Path in Bash
  • Extract Extension from File Path in Bash
  • Extract Filename without Extension from Full Path in Bash
  • Bash for Each File in a Directory
  • Bash for Loop with Array
  • Bash Continue – Using with for Loop
  • Bash Backticks vs Dollar Parentheses $()
  • How to Assign Variable in Bash
  • How to Assign Variable in Bash
  • Bash Division Explained
  • Bash Modulo (Division Remainder)
  • Bash While Read Line by Line
  • Bash shift Command
  • Bash Looping Through Array of Strings
  • Bash read Command with Examples
  • Bash Check Empty Array
  • Using Bash For Loops to Iterate Over a List of Strings
  • Bash Break – Using with For Loop
  • How to Use seq With for Loop in Bash
  • How to Use $@ in Bash Scripting
  • Get the Current Script Directory in Bash
Powered by GitBook
On this page
  • 1. Introduction
  • 2. Using basename
  • 3. Using parameter expansion
  • 4. Using powerful Bash tools
  • 5. Example Script

Extract Filename without Extension from Full Path in Bash

PreviousExtract Extension from File Path in BashNextBash for Each File in a Directory

Last updated 1 year ago

1. Introduction

In this tutorial, we delve into the world of bash scripting and explore techniques to extract filenames from file paths while trimming the extension. We cover essential tools like basename, parameter expansion, cut, awk, and sed, highlighting their unique features and applications. By mastering these methods, you'll gain the ability to efficiently manipulate filenames in bash scripts, enabling tasks such as renaming and copying files with ease.

2. Using basename

As we gathered, the sole role of the bash command is to give us the file name from the path. But if the file name has an extension, we get that too. We can still use the command to get rid of the or any suffix. This ensures we end up a clean output of just the name of the file.

Example:

basename /home/ubuntu/sample.txt .txt
basename -s .txt /home/ubuntu/sample.txt

We are aware the command accepts two syntaxes. They are just a different way of writing. In the first one we have to provide the path and the extension we want to be trimmed from the final output. In the latter case, we have the -s option which accepts the extension we want to remove.

We can, with certain limitations, combine -s and -a flag to trim extensions from multiple inputs.

Example:

basename -as .txt /home/ubuntu/readmecopy.txt /home/ubuntu/readme.txt
basename -as .txt /home/ubuntu/readmecopy.txt /home/ubuntu/config.cfg

In the first case, we have asked the command to remove the .txt extension from multiple inputs. But there is a loophole. We cannot assign individual suffices. That won’t work. Our hand is forced as we can only provide one suffix to all the file paths.

If multiple files with different extensions exist, then it will trim the ones whose extensions match with the supplied. In our case, we get "sample" and "sample.cfg" as the eventual output. They will be separated by a line break.

3. Using parameter expansion

We saw how parameter expansions work above and how we can use it to extract the file name with the extension. But we might only be interested in the file name. We can still proceed with it.

Example:

filepath=/home/ubuntu/sample.txt
filename=$(basename $filepath)
echo ${filename%.*}

The above example can be generalized as follows:

${variable%pattern}

This trims the shortest match from the end of the pattern. It displays whatever is after the pattern. The shortcoming again is that we need a variable because of the parameter expansion.

We have used the regex (*) as the pattern. We have also added the dot (.) before the regular expression. This dot represents the beginning of the extension.

Bash will look for all the characters from the last dot and trim it. Hence, we will get the characters before the final dot. This gives us just the name of the file.

4. Using powerful Bash tools

We talked about utilizing the powerful built-in tools to fetch the file name. They may appear complex at first. Let us understand them one by one and how we can integrate the regular expressions.

4.1 Using cut

Example:

path="/home/ubuntu/contents.tar.gz"
echo "$path" | cut -f 1 -d '.' | tr '/' '\n' | tail -1

Cut is a very straightforward tool. And it comes in handy in our case to extract the file name. We have combined it with some of the readily available tools- tr and tail.

To sum up our example:

  • Cut makes /home/ubuntu/contents.tar.gz become /home/ubuntu/contents

  • Tr breaks /home/ubuntu/contents into 3 different lines home ubuntu Contents

  • Tail prints contents

4.2 Using awk

Example:

path="/home/ubuntu/sample.txt"
echo "$path" | awk -F '/' '{sub(/[.][^.]*/,"",$NF); print $NF}'

In this example, we have taken a variable to hold the entire file path. We are free to supply that string directly to the echo command. We pipe the result of this echo command so that Bash can send it as an input to the awk command.

The -F option available with the awk command acts as the field separator. We have built the regular expression for the awk statement in such a way that the string dissociates into two halves from the final slash (/).

We then pick the last half of the field using $NF. This will contain the filename without the path information. Next, we call the available function with awk- sub(). This clears the file extension and prints the result as the filename.

4.3 Using sed

Example:

path="/home/ubuntu/sample.txt"
echo $path | sed 's|.*/||; s|[.][^.]*$||'

After that it is just a case of providing the right regular expression to the command. The s option with the flag is used to tell Bash that sed wants to carry out substitution. We have taken the symbol | to act as a separator for the sed syntax. This character may be uniformly replaced by any other single character within any given s command.

We have used two regular expressions here:

s|.*/|| : This expression ends up removing everything till the last slash. This is the parent directory of the file.

s|[.][^.]*$|| – This regular expression trims everything after the last dot. This gets rid of the file’s extension.

We can notice that the core regular expression /[^.]*$ in the above awk command and here has remained the same.

5. Example Script

As we have learned different ways of fetching the name of the file, we can look at an example that prompts the user to provide the complete path of the file. The script checks the existence of the file. It then asks the user to provide the extension so that the script can create a copy of the same file with a different extension.

#!/bin/bash read -p "Enter the filename with it's complete path: " fpath if [ ! -f "$fpath" ]then echo "Invalid file. Please check the input."else read -p "Enter the new extension you want: " ext fname=`basename $fpath` fname=`echo ${fname%.*}` dname=`dirname $fpath` cp $fpath ${dname}/${fname}.${ext} fi

2. The script checks if the file specified by fpath does not exist using the -f test condition. If the file doesn't exist, it prints "Invalid file. Please check the input."

3. If the file exists, the script prompts the user to enter the new extension. This response is stored in the ext variable.

5. It then uses the parameter expansion to get rid of the extension. This gives just the filename and the script overwrites the variable fname with this value.

6. Similarly, the script utilizes the dirname command to get the directory path from the user input present in fpath. The variable dname stores the output of the dirname command. 7. Finally, the script copies the file from the location in fpath to the destination ${dname}/${fname}/${ext}.

8. This ensures that the file is copied in the same location with the same name and with a user-provided extension.

In both cases, the should produce the result "sample.txt". As we have provided the suffix, the output will be trimmed further by removing the .txt from the result. We will get the final output "sample". If we provide an imaginary suffix or one that is not at the end, the outcome remains the same. It will assume there was no suffix.

In our example, we have using the basename command. Then we used the parameter expansion.

In our example, we pipe the result of the echo command so that Bash can send it as an input to the command. The cut command uses the delimiter (-d) as the dot (.) for extension. This into two parts- one before the dot and the other after.

The -f 1 option ensures that Bash opts for the former. This is then sent to the command which substitutes the path separator with line breaks. From this we have to pick the last line. Tail -1 does that job for us.

In this example as well, we store the entire file path in a variable. We can also feed the string directly to the echo command. We pipe the result of this echo command, which becomes the input to the .

1. The script starts by asking the user to enter the filename with its complete path using the . The entered value is stored in the fpath variable.

4. The script pulls out the name of the file with the extension using the basename command. Using technique, it stores the response of the command in the variable fname.

basename
extracted the filename from the file path
cut
splits the string
tr
sed command
read command
backticks
extension
Command output: 'sample' - basename /home/ubuntu/sample.txt .txt
usind sed - Extracting filename without extension
using awk - Extracting filename without extension
using parameter expansion - Extracting filename without extension
Output of basename command: removing '.txt' suffix from '/home/ubuntu/readmecopy.txt' and '/home/ubuntu/readme.txt'
cut command -  Extracting filename without extension
Bash script: Prompting user to enter a file path, changing extension, and copying the file