Extract Filename and Extension in Bash

Introduction

There are many reasons why you might want to extract the filename and extension of a file in Bash:

  • To manipulate the file name or extension - You may want to extract the filename or extension in order to modify it, such as adding a prefix or suffix to the file name, or changing the file extension.

  • To create a file with a unique name - You may want to extract the filename and extension in order to create a new file with a unique name, such as by adding a timestamp or a random number to the file name.

  • To use the filename or extension in a script or command - You may want to extract the filename or extension in order to use it as an argument or input for a script or command, such as to pass it to a program or to create a file with the same name as a directory.

  • To extract information from the file name or extension - You may want to extract the filename or extension in order to extract information from it, such as the date or the file type.

In this article, we'll take a look at the three most common ways you can extract filename and file extension in Bash. We'll take a look at each of them and give you the pros and cons, so you can make an educated decision on what approach suits you the best.

Method 1: Using the basename Command

The basename command can be used to extract the filename and extension from a file path:

filename=$(basename /path/to/file.txt)
echo $filename
# Output: "file.txt"

Although this method is quite simple and easy to use, unfortunately, there is no way we can extract only the file name (with no extension) without any post-processing.

You can also use the dirname command to extract the directory path separately:

directory=$(dirname /path/to/file.txt)
echo $directory
# Output: "/path/to"

Pros:

  • Simple to use
  • Handles filenames with spaces correctly

Cons:

  • Only extracts the filename and cannot extract the extension separately without additional processing

Method 2: Using Parameter Expansion

Bash provides a feature called parameter expansion, which allows you to extract parts of a variable using a special syntax. For example, you can use the following syntax to extract the filename and extension from a file path stored in a variable:

filepath="/path/to/file.txt"
filename=${filepath##*/}
echo $filename
# Output: "file.txt"

You can also use parameter expansion to extract the extension separately:

extension=${filename##*.}
echo $extension
# Output: "txt"

Pros:

  • Flexible
  • Can extract both the filename and extension separately,
  • Handles filenames with spaces correctly

Cons:

  • Requires a variable to store the file path

Method 3: Using the awk command

The awk command is a powerful text processing tool that can be used to extract parts of a string. For example, you can use the following syntax to extract the filename and extension from a file path:

filepath="/path/to/file.txt"
filename=$(echo $filepath | awk -F/ '{print $NF}')
echo $filename
# Output: "file.txt"
Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

You can also use awk to extract the extension separately:

extension=$(echo $filename | awk -F. '{print $NF}')
echo $extension
# Output: "txt"

Pros:

  • Powerful
  • Can extract both the filename and extension separately
  • Handles filenames with spaces correctly

Cons:

  • Syntax may be unfamiliar to some users
  • Requires piping the file path through awk

Conclusion

Overall, extracting the filename and extension of a file in Bash can be a useful technique for working with files and performing various tasks in the Bash shell.

Each of the mentioned methods has its own advantages and disadvantages, and the best choice will depend on your specific needs and preferences. It is often useful to be familiar with multiple approaches so that you can choose the one that is most suitable for your situation.

Last Updated: April 26th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms