# extracting only the filename without its extenstion

**URL:** <https://community.unix.com/t/extracting-only-the-filename-without-its-extenstion/155618>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [August 24, 2005, 2:44pm UTC](https://community.unix.com/t/extracting-only-the-filename-without-its-extenstion/155618 "2005-08-24T14:44:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lotus123](https://community.unix.com/letter_avatar/lotus123/32/5_5575768a8748004e209b776fc1b2916d.png) [@lotus123](https://community.unix.com/u/lotus123)\
**Post date:** [August 24, 2005, 2:44pm UTC](https://community.unix.com/t/extracting-only-the-filename-without-its-extenstion/155618/1 "2005-08-24T14:44:44Z")

</div>

Hi,

I have a requirement that i need to store only the filename without its extension. Can anyone please help me to do this.

For Example, i have stored the filename in a varialble called fname. I need to extract all the charecters before the first occurence of the dot.  
If fname has value as PG\_0008\_20050519.xml.gz then i need to extract only "PG\_0008\_20050519". Please help me to do this.

TIA.

---

<div class="post-metadata">

**Author:** ![98\_1LE](https://community.unix.com/letter_avatar/98_1le/32/5_5575768a8748004e209b776fc1b2916d.png) [@98\_1LE](https://community.unix.com/u/98_1LE)\
**Post date:** [August 24, 2005, 3:07pm UTC](https://community.unix.com/t/extracting-only-the-filename-without-its-extenstion/155618/2 "2005-08-24T15:07:55Z")

</div>

If the extension is always the same:

```nohighlight
fname=`basename $fname xml.gz`

```

If the extension is not always the same:

```nohighlight
fname=`echo $fname | cut -d\. -f1`
fname=${fname}.

```

---

<div class="post-metadata">

**Author:** ![lotus123](https://community.unix.com/letter_avatar/lotus123/32/5_5575768a8748004e209b776fc1b2916d.png) [@lotus123](https://community.unix.com/u/lotus123)\
**Post date:** [August 24, 2005, 3:22pm UTC](https://community.unix.com/t/extracting-only-the-filename-without-its-extenstion/155618/3 "2005-08-24T15:22:23Z")

</div>

Works Perfect.

Thanks a lot for the help.

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [August 25, 2005, 10:44am UTC](https://community.unix.com/t/extracting-only-the-filename-without-its-extenstion/155618/4 "2005-08-25T10:44:04Z")

</div>

some more ways

the first is one with awk  
echo `basename \<program-path\>` | awk -F"." '{print $1}'

this one is with sed but no of characters in extension need to be known in prior  
for 3 character extension  
echo `basename \<program-path\>` | sed -e 's/\....//'

advancement to the above method, no of characters in extension need not be known

echo `basename \<program-path\>` | sed -e 's/\..\*//'
