Create, Delete and Change Directories in Perl

PERLServer Side ProgrammingProgramming Scripts

You can use mkdir function in Perl to create a new directory. You will need to have the required permission to create a directory.

#!/usr/bin/perl
$dir = "/tmp/perl";
# This creates perl directory in /tmp directory.
mkdir( $dir ) or die "Couldn't create $dir directory, $!";
print "Directory created successfully\n";

Remove a directory

You can use rmdir function in Perl to remove a directory. You will need to have the required permission to remove a directory. Additionally this directory should be empty before you try to remove it.

#!/usr/bin/perl
$dir = "/tmp/perl";
# This removes perl directory from /tmp directory.
rmdir( $dir ) or die "Couldn't remove $dir directory, $!";
print "Directory removed successfully\n";

Change a Directory

You can use chdir function in Perl to change a directory and go to a new location. You will need to have the required permission to change a directory and go inside the new directory.

#!/usr/bin/perl
$dir = "/home";
# This changes perl directory and moves you inside /home directory.
chdir( $dir ) or die "Couldn't go inside $dir directory, $!";
print "Your new location is $dir\n";
raja
Published on 29-Nov-2019 11:39:54
Advertisements