Sentry Answers>Bash>

Extract filename and extension from a string in Bash

Extract filename and extension from a string in Bash

David Y.

The Problem

Given a file path string in Bash, how can I reliably extract the filename and extension, even in cases where the name contains multiple .s? For example, I would like to extract the name “requirements.updated” and extension “txt” in separate variables from this string:

Click to Copy
/home/user/requirements.updated.txt

The Solution

We can achieve this in Bash using the basename command and parameter expansion.

First, extract the filename from the path using basename:

Click to Copy
filepath="/home/user/requirements.updated.txt" filename_with_ext=$(basename "$filepath")

Next, use parameter expansion to separate the filename and extension:

Click to Copy
filename="${filename_with_ext%.*}" extension="${filename_with_ext##*.}"

In the first line, the ${filename_with_ext%.*} parameter expansion will remove the shortest match of .* from the end of $filename_with_ext, removing only the final . and text after it.

In the second line, the ${filename##*.} parameter expansion will remove the longest match of *. from the beginning of $filename_with_ext, leaving us with just the text after the final ..

Our full Bash script will look like this:

Click to Copy
#!/bin/bash filepath="/home/user/requirements.updated.txt" filename_with_ext=$(basename "$filepath") filename="${filename_with_ext%.*}" extension="${filename_with_ext##*.}" echo "Path: $filepath" echo "Filename with extension: $filename_with_ext" echo "Filename without extension: $filename" echo "File extension: $extension"

When run, this script will produce the output below:

Click to Copy
Path: /home/user/requirements.updated.txt Filename with extension: requirements.updated.txt Filename without extension: requirements.updated File extension: txt
  • Syntax.fmListen to the Syntax Podcast
  • ResourcesWhat is Distributed Tracing
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.