# Working with a filename

**URL:** <https://community.unix.com/t/working-with-a-filename/154104>\
**Category:** Shell Programming and Scripting\
**Created:** [June 17, 2005, 4:23am UTC](https://community.unix.com/t/working-with-a-filename/154104 "2005-06-17T04:23:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![steiner](https://community.unix.com/letter_avatar/steiner/32/5_5575768a8748004e209b776fc1b2916d.png) [@steiner](https://community.unix.com/u/steiner)\
**Post date:** [June 17, 2005, 4:23am UTC](https://community.unix.com/t/working-with-a-filename/154104/1 "2005-06-17T04:23:43Z")

</div>

Hello,

I am trying to work with filename in Unix.  
So in my example a have a filename like : ABC\_totoalerte.txt and I want just to capture totoalerte.txt.

How can i do it , i check it with cut -c,4-\* but it didn't work.

Can you help me about this problem. Thanks..

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [June 17, 2005, 5:03am UTC](https://community.unix.com/t/working-with-a-filename/154104/2 "2005-06-17T05:03:08Z")

</div>

This solution is very specific to the format you gave i.e. ABC\_anyfilename.extension

```nohighlight
echo ABC_totoalerte.txt | awk -F"_" '{ printf $2 }'

```

Vino

---

<div class="post-metadata">

**Author:** ![rahul123\_libra](https://community.unix.com/letter_avatar/rahul123_libra/32/5_5575768a8748004e209b776fc1b2916d.png) [@rahul123\_libra](https://community.unix.com/u/rahul123_libra)\
**Post date:** [June 17, 2005, 5:15am UTC](https://community.unix.com/t/working-with-a-filename/154104/3 "2005-06-17T05:15:47Z")

</div>

Try This

But this is also for a specific structure :-

sed 's/^[a-z]\*\_\([a-z]\*[0-9]\*[.][a-z]\*\)/\1/' file.txt

---

<div class="post-metadata">

**Author:** ![ssk](https://community.unix.com/letter_avatar/ssk/32/5_5575768a8748004e209b776fc1b2916d.png) [@ssk](https://community.unix.com/u/ssk)\
**Post date:** [June 17, 2005, 7:28am UTC](https://community.unix.com/t/working-with-a-filename/154104/4 "2005-06-17T07:28:04Z")

</div>

🙂  
echo ABC\_totoalerte.txt | cut -d"\_" -f2

should work.

---

<div class="post-metadata">

**Author:** ![zazzybob](https://community.unix.com/user_avatar/community.unix.com/zazzybob/32/112_2.png) [@zazzybob](https://community.unix.com/u/zazzybob)\
**Post date:** [June 18, 2005, 7:45am UTC](https://community.unix.com/t/working-with-a-filename/154104/5 "2005-06-18T07:45:13Z")

</div>

Use a good shell (ksh, bash) and then use the shell's built-in features to do this lightning-fast...

```nohighlight
$ FILENAME="ABC_totoalerte.txt"
$ FILENAME=${FILENAME##*_}
$ echo $FILENAME
totoalerte.txt

```

Cheers  
ZB

---

<div class="post-metadata">

**Author:** ![Lestat](https://community.unix.com/user_avatar/community.unix.com/lestat/32/152_2.png) [@Lestat](https://community.unix.com/u/Lestat)\
**Post date:** [June 20, 2005, 12:03pm UTC](https://community.unix.com/t/working-with-a-filename/154104/6 "2005-06-20T12:03:51Z")

</div>

If you always want to get the last part after '\_' do this

```nohighlight

ls -1 | cut -d "_" -f2

```
