LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 05-22-2006, 07:38 AM   #1
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Rep: Reputation: 15
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 07:55 AM.
 
Old 05-22-2006, 08:14 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
Old 05-22-2006, 02:44 PM   #3
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Original Poster
Rep: Reputation: 15
I love you.
 
Old 05-23-2006, 01:32 AM   #4
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
*Or* (I don't see much reason to do this but..) you can use the dirname(1) utility.
 
Old 05-23-2006, 10:25 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 09-02-2008, 02:53 PM   #6
vammydhar
LQ Newbie
 
Registered: Sep 2008
Posts: 1

Rep: Reputation: 0
thanks

Thanks for the above soultion
 
Old 09-02-2008, 03:12 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
:-)

You are welcome!
 
1 members found this post helpful.
Old 04-11-2012, 01:25 AM   #8
isisira
LQ Newbie
 
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
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!
 
Old 04-11-2012, 12:13 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
  


Reply

Tags
basename, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing directory path from tar archives haddad Linux - Software 1 01-18-2006 09:02 AM
Removing a Directory from the Terminal without deleting each file or folder lennysokol Linux - General 4 05-31-2005 12:30 AM
Removing path form PATH maginotjr Linux - Newbie 3 02-12-2005 03:23 PM
removing one directory in PATH env chunlee Linux - Newbie 4 11-06-2004 08:50 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM


All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration