Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to remove the subdirectory or main directory wherever the directory name starts with temp.How can i do it using perl?

What I have tried:

#!/usr/local/bin/perl
use File::Path;
$files_deleted=rmtree('/home/rperiasa/output/*/*/temp*');
#print $files_deleted;
unlink $files_deleted;
Posted
Updated 1-Mar-17 23:42pm

1 solution

You have to iterate over the directories.

A possible solution:
PERL
use File::Basename;

sub ProcessDirectory{
    my ($workdir) = shift;
    print "Processing '$workdir'\n";
    my $mask = $workdir . '/*';
    # glob: Return list of filename expansions for search mask
    # grep -d: Extract directories from list
    my @dirs = grep { -d } glob $mask;
    foreach $d (@dirs)
    {
        # Process sub directories
        ProcessDirectory($d);
        my $dirname = basename($d);
        if ($dirname =~ /^temp*/)
        {
            print " Removing $d\n";
            my $filemask = $d . '/*';
            # Delete all files in directory and then the directory
            #unlink glob $filemask;
            #rmdir $d;
        }
    }
}

ProcessDirectory('/home/rperiasa/output');

Note that I have commented the remove operation so that you can check the operation first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900