How to remove pathname from a filename

Hi Experts,

I want to remove the pathname from a filename.

./path/name/file.txt
I want to change this to just file.txt ( everything upto the last / should be removed)

How can I do it.

Thanks,
ambuliAsked:
Who is Participating?
 
tel2Connect With a Mentor Commented:
Hi ambuli,

In Perl, let's assume the path is in a $pathname variable:
    $pathname = './path/name/file.txt';
You could then do this:
    ($filename = $pathname) =~ s|.*/||/;
    print $filename;
The (tested) output is:
    file.txt
0
 
ambuliAuthor Commented:
Thanks you
0
 
tel2Commented:
Sorry, remove the last "/", so the substitution line reads:
    ($filename = $pathname) =~ s|.*/||;
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
ambuliAuthor Commented:
Hi tel2,

I am getting syntax errors.

syntax error at searchLogs.pl line 51, near "/;"
0
 
tel2Commented:
I know. ambuli.  Did you read my 2nd post, above?
0
 
ambuliAuthor Commented:
Sorry, I did not check that before.  Yes, it is working fine.  Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.