Skip to main content
  1. About
  2. Stack Internal
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Extract filename and extension in Bash

I want to get the filename (without extension) and the extension separately.

The best solution I found so far is:

NAME=`echo "$FILE" | cut -d'.' -f1`
EXTENSION=`echo "$FILE" | cut -d'.' -f2`

This is wrong because it doesn't work if the file name contains multiple . characters. If, let's say, I have a.b.js, it will consider a and b.js, instead of a.b and js.

It can be easily done in Python with

file, ext = os.path.splitext(path)

but I'd prefer not to fire up a Python interpreter just for this, if possible.

Any better ideas?

Answer*

Reminder: Answers generated by AI tools are not allowed due to Stack Overflow's artificial intelligence policy

Draft saved
Draft discarded

Required fields are marked with *

Cancel
7
  • 2
    Although not a direct answer to the original post, this is by far the most sensible response. Thanks for providing it.
    mp035
    –  mp035
    2021-01-06 05:23:30 +00:00
    Commented Jan 6, 2021 at 5:23
  • I really appreciate this thorough answer highlighting common built-ins. Though I ended up just doing this in python using the -c flag, if I were constrained to using just shell scripting I'd use the concepts outlined herein. Thank you!
    Jason R Stevens CFA
    –  Jason R Stevens CFA
    2022-04-10 02:26:30 +00:00
    Commented Apr 10, 2022 at 2:26
  • @JasonRStevensCFA under python, you will use python-magic library!
    F. Hauri - Give Up GitHub
    –  F. Hauri - Give Up GitHub
    2022-04-10 05:52:22 +00:00
    Commented Apr 10, 2022 at 5:52
  • @F.Hauri Cool lib, thanks for sharing. I just use standard stuff as the string builtins for scripting are beyond simple. For example, $(python -c "'$1'.split('/')[-1]") will get you the filename with extension from a path string variable $1 using a subshell (I use it like this in some local scripts). I don't use this kind of "magic" in prod but these features of the Python language are fantastic for simple task-based things.
    Jason R Stevens CFA
    –  Jason R Stevens CFA
    2022-04-10 20:15:18 +00:00
    Commented Apr 10, 2022 at 20:15
  • @JasonRStevensCFA Using forks to python, like any other language (perl, awk, etc...) for so tiny requirment is counter-productive! Try to run same fork 1000 time and compare with parameter expansion...
    F. Hauri - Give Up GitHub
    –  F. Hauri - Give Up GitHub
    2022-04-11 21:27:44 +00:00
    Commented Apr 11, 2022 at 21:27

lang-bash
Morty Proxy This is a proxified and sanitized view of the page, visit original site.