Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting just the file name from full path

Tags:

regex

perl

I need to get from a full file path just the name of the file. I've tried to use:

$out_fname =~ s/[\/\w+\/]+//;

but it "eats up" also purts of the file name.

example:

for a file: /bla/bla/folder/file.part.1.file, it returned: .part.1,file

like image 909
SIMEL Avatar asked Nov 28 '22 23:11

SIMEL


2 Answers

You can do:

use File::Basename;

my $path = "/bla/bla/folder/file.part.1.file";
my $filename = basename($path);
like image 161
codaddict Avatar answered Dec 06 '22 05:12

codaddict


Besides File::Basename, there's also Path::Class, which can be handy for more complex operations, particularly when dealing with directories, or cross-platform/filesystem operations. It's probably overkill in this case, but might be worth knowing about.

use Path::Class;

my $file = file( "/bla/bla/folder/file.part.1.file" );
my $filename = $file->basename;
like image 20
plusplus Avatar answered Dec 06 '22 04:12

plusplus