HOWTO · Bash
How to Remove a File Extension in Bash
Remove a filename's final extension in Bash, use basename for a known suffix, or use cut to extract delimited text.
On this page
To remove the final extension from a filename string in Bash, use parameter expansion: name="${file%.*}". The %.* pattern removes the shortest match of a dot followed by any characters from the end, so earlier dots remain. This changes a string only; it does not rename a file on disk.
Run this example:
file='annual.report.html'
name="${file%.*}"
printf '%s\n' "$name"
Remove an extension with Bash parameter expansion
Parameter expansion is usually the simplest choice when the filename is already in a Bash variable. It needs no external command or pipeline. Assign the result to a new variable to keep the original value available, or assign back to the same variable when you no longer need it. Quote variable expansions so spaces and wildcard characters remain part of the filename text.
The pattern removes the final dot and everything after it. For example, applying it to annual.report.html gives annual.report; applying it to notes leaves notes unchanged because there is no dot to match. A leading-dot name such as .profile becomes an empty string with this pattern, so treat leading-dot files separately if they are in scope for your data. The rule is textual: it does not determine whether a suffix is a meaningful file type.
Use basename when the extension is known
Use basename when you know the exact suffix to remove. Its second argument is a suffix to strip only when it matches the end of the name. For instance, basename "$file" .html turns annual.report.html into annual.report. If the name ends in .HTML or another suffix, .html does not match and is retained. Quoting the variable also protects spaces in the input.
basename is an external command, so it is less direct than parameter expansion for a value already in a Bash variable. It is useful when the known-suffix behavior is exactly what you want. Neither form changes the file on disk.
Use cut for delimited text, not general filename suffixes
The cut command is designed to select fields from delimiter-separated text. With cut -d '.' -f 1, each input line is split at periods and only the first field is kept. This can help process a list when the intended rule is “keep the text before the first period.” It is not a general way to remove the final filename extension: annual.report.html becomes annual, losing both .report and .html.
For a newline-separated list, pipe the text into cut, for example: printf '%s\n' "$file" | cut -d '.' -f 1. The -d option chooses the delimiter, and -f 1 selects the first field. Use cut when the input is structured around a delimiter and that field selection is intended. For a final suffix, prefer Bash parameter expansion instead.
Handle filenames with multiple dots and choose an approach
A filename with several dots makes the difference clear. For archive.part.tar.gz, ${file%.*} removes only .gz, leaving archive.part.tar; basename "$file" .gz does the same when .gz is the known suffix; cut -d '.' -f 1 keeps only archive. Choose parameter expansion for the final suffix, basename for a known suffix, and cut for a delimited field. These examples transform filename text; renaming a filesystem entry requires a separate operation.