Search code examples
perlunixfilesystems

deleting files and folders in a specified directory


#!/usr/bin/perl

# purge backups older than 210 days

use strict;
use warnings;

use File::Find::Rule;
use File::Path 'rmtree'; # listed directory has files and folders

# to delete files and folders in the specified directory age > 210 days

my $dir = '/volume1/Backup01/*/Archived_files/';
my $days = 210;

# Do i need to input something like @folder = File::Path *** ??

my @files = File::Find::Rule->file()
                            ->maxdepth(1) # maxdepth(0) will allow me to delete files in subdirectories as well?
                            ->in($dir)

# How can I make a for loop to look for folders whose -M > 210 and allow me to delete?
                            
for my $file (@files){
    if (-M $file > 210){
        unlink $file or warn $!;
    }
}

Included comments for what I need... background is purging old files in a NAS server and quite lost at the moment as to how to safely purge away a few thousand files and folders >.<


Solution

  • use warnings;
    use strict;
    use feature 'say';
    
    use File::Find::Rule;    
    use FindBin qw($RealBin);
    
    my $dir = shift // $RealBin;   # start from this directory
    
    my @old_entries = File::Find::Rule -> new 
        -> exec( sub { -M $_[2] > 210 } ) 
        -> in($dir); 
    
    say for @old_entries; 
    

    This finds all entries (files and directories), recursively, that are older than 210 days.

    Now go through the list and delete. Can identify directories using again -X filetests (-d), and it may be a good idea to first delete non-directories and then (now empty) directories. Or use rmtree as intended and skip files that would've been in those directories.


    For example, something like

    if ($old_entries[0] eq $dir) { 
        say "Remove from this list the top-level dir itself, $old_entries[0]";
        shift @old_entries;
    }
    
    my $del_dir;
    
    for my $entry (@old_entries) {
        if (-d $entry) {
            $del_dir = $entry;
            say "remove $entry";  # rmtree
        }    
        # Skip files other than the ones at the top level
        elsif ($del_dir and $entry =~ /^$del_dir/) {
            say "\tskip $entry";  # its directory is removed
        }
        else { 
            say "Remove top-level file: $entry";  # unlink
        }
    }
    

    Note -- this has not been fully tested

    Note -- make sure to not remove the top-level directory, in which you start the search!