# Bash: Extract Filename and Extension from a File Path

> Extract the filename and extension from a file path string in Bash using the basename command and parameter expansion operators for reliable splitting

**URL:** https://sentry.io/answers/extract-filename-and-extension-from-a-string-in-bash/

---

## 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:

```
/home/user/requirements.updated.txt
```

## The Solution

We can achieve this in Bash using the `basename` command and [parameter expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html).

First, extract the filename from the path using `basename`:

```bash
filepath="/home/user/requirements.updated.txt"
filename_with_ext=$(basename "$filepath")
```

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

```bash
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:

```bash
#!/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:

```
Path: /home/user/requirements.updated.txt
Filename with extension: requirements.updated.txt
Filename without extension: requirements.updated
File extension: txt
```

---

*Source: [sentry.io/answers/extract-filename-and-extension-from-a-string-in-bash/](https://sentry.io/answers/extract-filename-and-extension-from-a-string-in-bash/)*
