- Perl ›
- builtin functions ›
- here
rmdir function - delete directory
Use the rmdir function to delete a directory. You can only delete empty directories that do not contain files. If it succeeds, it returns true, and if it fails, it returns false. If it fails, the error content is stored in $!.
# Delete directory rmdir $dir;
Example
This is an example to delete a directory using the rmdir function.
use strict; use warnings; # Delete the directory # rmdir $dir # Preparation (creating a directory) my $dir = "dir_20080526_$$"; mkdir $dir or die "Unable to create $dir.:$!"; print "Preparation: $dir has been created.\n\n"; print "1: Delete the directory.\n"; rmdir $dir or die "Unable to delete $dir.:$!"; print "$dir has been deleted.\n";