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. Why do we need to extract?
  • 3. Extract filename with extension
  • 4. Example Script
  • 5. Conclusion

How to Extract Filename from Absolute Path in Bash

PreviousTrimming White Space in BashNextHow to Get Directory Path in Bash

Last updated 1 year ago

1. Introduction

Whenever dealing with absolute paths, we will come across situations where we just need the name of the file. What do we do then? Definitely, we can use the available tools in Bash like awk.

We also have tailor-made tools whose sole purpose is to extract the final component in the file path. Basename is one such command-line utility. Another popular technique is the parameter expansion for removing the directory and trailing suffix from file names. In this tutorial, we will also examine the drawbacks of the different approaches and look at a real-world problem.

2. Why do we need to extract?

The obvious question is why to extract? We have multiple reasons to do so.

  • To rename the file or extension - We may want to extract the filename or extension to rename it with a new name or extension.

  • To have a new file name with the timestamp - We generally have log files in Linux with the same file name and extension. But the file name gets a suffix as the timestamp.

3. Extract filename with extension

We will focus on three of the most common ways by which we can extract the filename with its extension in Bash.

3.1 Using basename

With Bash we can employ many utilities to extract the filename and the extension from the given file path. But, why go to such great lengths when we have a tailor-made tool whose sole purpose is to fetch the final component in the file path. is a command-line tool for removing the directory and trailing suffix from file names.

Example:

basename /home/ubuntu/sample.txt
basename /home/ubuntu

Basename is a simple tool to implement. All it does is finds the last path separator (/). It displays whatever is present after that. As we can notice, the command is totally unbiased when it comes to files or directories. Its job is to simply give us the information after the final slash.

To make the command interpret more than one path, we supply -a. We can also give the --multiple flag. It is used to achieve the same. Then comes a list of files, each separated by a space.

Example:

basename -a /home/ubuntu/sample.txt /home/ubuntu/sample1.cfg

In our example, we will get the name of the two files with their respective extensions, sample.txt and sample1.cfg from their respective paths. These two will appear in separate lines.

To separate these outputs with NULL rather than a newline we have to pass the -z flag along with it.

Example:

basename -az /home/ubuntu/sample.txt /home/ubuntu/sample1.cfg

One major drawback of this command is that it will extract the filename with the extension. We cannot use it just to extract the extension or the directory path.

3.2 Using parameter expansion

Example:

filepath=/home/ubuntu/sample.txt
echo ${filepath##*/}

The above example can be generalized as follows:

${variable##pattern}

This trims the longest match from the beginning till the pattern. It displays whatever is after the pattern. The only drawback is that we have to use a variable since this is a parameter expansion.

In our example, we have used the regex (*) as the pattern. In the pattern we have also added the path separator (/). It will look for all the characters till the last slash (/) and trim it. Hence, we will get the characters after the final path separator. The only drawback here is that we cannot use the string directory. There is no other option but to store the string in a variable.

3.3 Using awk

The awk command was developed by Aho, Weinberger, and Kernighan and gets its name from their initials. It is a powerful, widely used Linux tool that lets us process and manipulate data. It also supports performing arithmetic operations. We can also take its help to extract the filename with its extension.

Example:

filepath=/home/ubuntu/sample.txt echo $filepath | awk -F/ '{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 (/). $NF ensures that Bash only prints the last column in the record.

4. 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 directory where he wants to keep a backup of this file with the same name and extension.

Example:

#!/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 complete path where you want to copy it: " fdir if [ ! -d "$fdir" ] then echo "Invalid new directory. Please check the input."else fname=`basename $fpath` cp $fpath ${fdir}/${fname} fi fi

1. The script begins by prompting the user to enter the filename with its complete path. It does it by employing the read command. The user input is stored in the fpath variable.

2. The script verifies the existence of the file specified in fpath 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 again prompts the user to enter the complete path of the destination directory where the file should be copied to. The entered value is stored in the fdir variable.

4. It then checks if the directory specified by fdir does not exist using the -d test condition. If the directory doesn't exist, it prints "Invalid new directory. Please check the input."

5. If both the file and the directory exist, the script uses the basename command to extract the filename from the given file path and stores it in the fname variable.

6. Finally, the cp command is used to copy the file specified by fpath to the destination directory ${fdir}/${fname}.

5. Conclusion

  • We can fetch the file name along with its extension from the absolute file path.

  • Basename is a simple enough command that can be used with certain limitations to pull out the filename.

  • With parameter expansions, we can extract parts of a variable and name of the file

  • We have seen the use of certain readily available powerful tools like awk.

Bash has an interesting feature named parameter expansion. With , we can extract parts of a variable. It has a unique syntax. This won't be available with other programming languages.

parameter expansions
Basename
using parameter expansion to extract filename from path
using awk to extract filename from path
describe the basenames of the given paths /home/ubuntu/sample.txt and /home/ubuntu/sample1.cfg respectively using -az option
example script to extract filename from path.
The output of 'basename /home/ubuntu/sample.txt' command is 'sample.txt and The output of 'basename /home/ubuntu' command is 'ubuntu'
The output of 'basename -a /home/ubuntu/sample.txt /home/ubuntu/sample1.cfg' command is sample.txt and sample1.cfg