Operations On Directories


Creating a Directory: #!/usr/bin/perl -w #mkdir syntax #mkdir (dirname, permissions) #dirname can be a full path mkdir ("TESTDIR", 0777); Removing a directory: #!/usr/bin/perl -w #rmdir syntax #rmdir (dirname) rmdir ("TESTDIR"); Printing contents of a directory: #!/usr/bin/perl -w opendir(HOMEDIR, "../PERL") || die ("NO DIRECTORY WITH THAT NAME"); @files = readdir(HOMEDIR); closedir(HOMEDIR); foreach $file (sort @files){ print ("$file\n"); } Listing Contents of a Folder #!/usr/bin/perl -w use strict; use BeginPerlBioinfo; my @files = (); my $folder = 'pdb'; unless(opendir(FOLDER, $folder)){ print "Cannot open folder $folder!\n"; exit; } @files = readdir(FOLDER); closedir(FOLDER); print join("\n", @files), "\n"; exit; List folders and subfolders #!/usr/bin/perl -w use strict; use BeginPerlBioinfo; my @files = (); my $folder = 'pdb'; unless(opendir(FOLDER, $folder)){ print "Cannot open folder $folder!\n"; exit; } @files = grep(!/^\.\.?/, readdir(FOLDER)); closedir(FOLDER); foreach my $file(@files){ if(-f "$folder/$file"){ print "$folder/$file\n"; }elsif(-d "$folder/$file"){ my $folder = "$folder/$file"; unless(opendir(FOLDER, "$folder")){ print "Cannot open folder $folder!\n"; exit; } my @files = grep (!/^\.\.?$/, readdir(FOLDER)); closedir(FOLDER); foreach my $file (@files) { print "$folder/$file\n" } } } exit; Recursive subroutine to list filesystem #!/usr/bin/perl -w use strict; list_recursively('pdb'); exit; sub list_recursively{ my($directory) = @_; my @files = (); unless(opendir(DIRECTORY, $directory)){ print "Cannot open directory $directory!\n"; exit; } @files = grep(!/^\.\.?$/, readdir(DIRECTORY)); closedir(DIRECTORY); foreach my $file (@files) { if(-f "$directory/$file"){ print "$directory/$file\n"; }elsif(-d "$directory/$file"){ list_recursively("$directory/$file"); } } }
ASSIGNMENT: