Extracting a substring starting from last occurance of a string/character

Hi All,
This is Ram. I'm new to this forum & new to shell scripts as well. I've a requirement in which I want to extract a substring from a given string based on last occurance of a character.

for eg.
I have a string of a file name with absolute path like
$filename=/aaa/bbb/ccc/ddd/xyz.txt

I want to extract only the actual file name ie, xyz.txt

The no. of directories ie, / will be dynamic. So I want to find the last occurance of / in the given string & to extract the string after the last /

can I have some ideas / suggestions please.

Thanks ,
Ram.

Ram,

You can use "basename" keyword in Unix.

for eg:

if you have a file (test1) with the content like below:

/aaa/bbb/ccc/ddd/xyz.txt
/bbb/ccc/ddd/123.txt

then your command must be like this:

for i in `cat test1`
do
File_Name=`basename $i`
echo $File_Name
done

Output:
xyz.txt
123.txt

$ pathname=/a/b/c/d/e/f/g/abc.dat
$ echo $pathname
/a/b/c/d/e/f/g/abc.dat
$ echo ${pathname##*/}
abc.dat
$