Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
05-22-2006, 06:38 AM
#1
Member
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80
Rep:
Perl, Removing file from path to get directory only
I need to remove the file name from a path, eg:
$fpath = /home/user/hello/hey.pl
So I can make it...
$fpath2 = /home/user/hello
Instead.
Is there an easy way to do this, like some inbuilt function?
Last edited by xemous; 05-22-2006 at 06:55 AM .
05-22-2006, 07:14 AM
#2
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Hi,
Take a look at:
File::Basename
It can do a lot more then you ask, with that in mind here's a code snippet:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
my $fpath = "/home/user/hello/hey.pl";
print "Original : " . $fpath . "\n";
my($filename, $directories) = fileparse($fpath);
print "Filename : " . $filename . "\n";
print "Directory : " . $directories . "\n";
Hope this helps.
1 members found this post helpful.
05-22-2006, 01:44 PM
#3
Member
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80
Original Poster
Rep:
I love you.
05-23-2006, 12:32 AM
#4
Member
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700
Rep:
*Or* (I don't see much reason to do this but..) you can use the dirname(1) utility.
05-23-2006, 09:25 AM
#5
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Hi,
True, you could use dirname (1), but it's an external (unix) command, seen from perl that is.
dirname [File::Basename] (3) is also part of File::Basename and an internal perl command, which is more resource friendly.
Hope this clears things up a bit.
09-02-2008, 01:53 PM
#6
LQ Newbie
Registered: Sep 2008
Posts: 1
Rep:
thanks
Thanks for the above soultion
09-02-2008, 02:12 PM
#7
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
:-)
You are welcome!
1 members found this post helpful.
04-11-2012, 12:25 AM
#8
LQ Newbie
Registered: Apr 2012
Posts: 1
Rep:
Thank you
Hi! Thank you.. A colleague today asked me how to do this and I directly got the answer when i came here..perfectly worked!
04-11-2012, 11:13 AM
#9
Senior Member
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,506
The Perl code to do this can be as simple as (if
$filename contains fully qualified filespec):
Code:
$filename=~m/^.+\//;
$path=$&;
Relies on greedy matching that is default in Perl.
--- rod.
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 06:39 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News