Bootstrap FreeKB - Perl (Scripting) - File and Directory basename and dirname
Perl (Scripting) - File and Directory basename and dirname

Updated:   |  Perl (Scripting) articles

In this example, the $file variable contains the absolute path to example.txt.

my $file = "/home/john.doe/example.txt";

 

Printing the $file variable . . . 

print "$file \n";

 

. . . will return the absolute path to example.txt.

/home/john.doe/example.txt

 

File::Basename can be used to parse the dirname and basename. In this example . . . 

  • $dirname = /home/john.doe
  • $basename = example.txt
my $file     = "/home/john.doe/example.txt";
my $dirname  = File::Basename::dirname($file);
my $basename = File::Basename::basename($file);

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b0f089 in the box below so that we can be sure you are a human.