Perl: getting the path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: getting the path
# 1  
Old 02-09-2005
Question Perl: getting the path

How do I strip of the path from the complete filename

for example

$file = "/home/docs/hw1.doc";

how do I extract the "/home/docs/" part?
# 2  
Old 02-10-2005
There are two ways of doing this - one with File::Basename (easy) and the other using regular expressions (portable but trickier for the uninitiated).

I've illustrated both methods below - change the shebang line as required.

Code:
#!/usr/bin/perl

use File::Basename;

$file = "/home/docs/hw1.doc";

$dirname = dirname( $file );
$filename = basename( $file );

# Append a trailing slash to $dirname - if that's what you want...
print( "Directory path: $dirname/\n" );
print( "Filename: $filename\n" );

# Without File::Basename, use regular expressions....
# There are many ways to do this - this is just one.
$file = "/another/path/here.doc";

$dirname = $filename = $file;
$dirname =~ s!^(.*/)[^/]*$!\1!;
$filename =~ s!^.*/([^/]*)$!\1!;

print( "Directory path: $dirname\n" );
print( "Filename: $filename\n" );

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (3 Replies)
Discussion started by: kiran425
3 Replies

2. UNIX for Dummies Questions & Answers

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (2 Replies)
Discussion started by: kiran425
2 Replies

3. Shell Programming and Scripting

Sizeof a file from directory path in perl

Hai how to find size of a file?? ex : /home/kiran/pdk/sample/calibre this is a path In that I have to find size of a files in side a calibre(it is the folder) like .results or .summary (1 Reply)
Discussion started by: kiran425
1 Replies

4. Shell Programming and Scripting

Editing path in a HTML file using Perl

Hello I want to replace the path to which a hyperlink points to. I have a html file <TABLE BORDER CELLPADDING=7, border=0><TR><td>Jun-10-2013_03_19_07_AM</td><td>Ank_Insert_1</td><td><b>FAILED: 1</b></td><td><A ... (14 Replies)
Discussion started by: ankurk
14 Replies

5. Shell Programming and Scripting

Shell or Perl (adding quote around path)

Hi Experts, I have a line as below: Text 351:2139 /opt/bo/boexi30/bobje/data/.bobj/registry/software/business objects/suite 12.0/olap intelligence/occa(o) What I need is: add a single quote around the path as below: chown 351:2139... (4 Replies)
Discussion started by: apatil65
4 Replies

6. Shell Programming and Scripting

How to search/replace a directory path in a file using perl

Hello All, Here is what I am trying to do and maybe you guys can point me in the right direction. I have a file that contains the following string(s): WARNING: </d1/test/program1> did not find item1 WARNING: </d1/test/program1> item1 does not exist WARNING: </d1/test/program2> item1 failed... (1 Reply)
Discussion started by: maxshop
1 Replies

7. UNIX for Dummies Questions & Answers

How to find perl installed path?

Hi all, how do we find where perl is installed via command line? Please let me know for both windows and unix platforms. when i tried in windows by following option. it is throwing error. C:\Users\dkanagar>whereis perl 'whereis' is not recognized as an internal or external command,... (4 Replies)
Discussion started by: Divakar
4 Replies

8. Shell Programming and Scripting

Copy last modified directory from path using Perl

Hi I need to copy last modified directory from path /users/bin to be copied to /tmp in a perl script ...somehow my script is not allowing me to do cd /users/bin. i have tried $dir = `find /users/bin -maxdepth 1 -type d -mtime 0 ` cp -R $dir /tmp/new but it says missing destination... (1 Reply)
Discussion started by: shaveta
1 Replies

9. Shell Programming and Scripting

Perl directory path in array

Hi anyone can help how put the directory in array in perl.eg directory paths below:- /home/user/ /home/admin/ /var/log/ IF path eq /home/user/ then the files moved to /data/user/ IF path eq /var/log/ then the files moved to /data/log/ Thanks (1 Reply)
Discussion started by: netxus
1 Replies

10. UNIX for Dummies Questions & Answers

How to get file in the path by using perl

Hi, I had path like this abc/def/file it was assigned to one varible. i want to get the "file" from the above path by using perl appreciate the help!!! chaitanya. (4 Replies)
Discussion started by: chaitubek
4 Replies
Login or Register to Ask a Question