#!/usr/bin/perl # Author: Alan J. Pippin # Description: Find and remove TV recording directories that don't contain any recordings in them use File::Copy; use File::Basename; use Getopt::Std; use File::stat; use Time::localtime; use File::Pid; use Data::Dumper; #################################################################################################### # Configuration parameters my $tv_src_dir = "/tvpool/tv"; my $log = "/var/log/organize/remove_empty_tv_dirs.log"; my $tv_file_ext = "-iregex \".*\.mov\" -o -iregex \".*\.avi\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mpg\" -o -iregex \".*\.ts\" -o -iregex \".*\.mkv\""; my $find_tv_show_dirs = "find \"$tv_src_dir/\" -maxdepth 1 -cmin +14400 -type d"; my $find_tv_season_dirs = "find \"$tv_src_dir/\" -maxdepth 2 -mindepth 2 -cmin +14400 -type d"; my $find_tv_shows = "find \"$tv_src_dir/\" -cmin +14400 $tv_file_ext"; #################################################################################################### # Find empty TV show directories to remove my %tv_show_dirs; # Find all of our TV season dirs and store them in a hash foreach $dir (`$find_tv_season_dirs`) { chomp($dir); my $season = basename($dir); my $show = basename(dirname($dir)); #print "-> Found dir $dir show $show season $season\n"; $tv_show_dirs{$show}{$season}{has_recordings} = 0; } foreach $dir (`$find_tv_show_dirs`) { chomp($dir); my $show = basename($dir); #print "-> Found dir $dir show $show\n"; $tv_show_dirs{$show}{has_recordings} = 0; } # Find all of our TV show recordings and store them in a hash foreach $file (`$find_tv_shows`) { chomp($file); my $recording = basename($file); my $season = basename(dirname($file)); my $show = basename(dirname(dirname($file))); #print "-> Found show $show season $season and recording $recording\n"; $tv_show_dirs{$show}{$season}{has_recordings} = 1; $tv_show_dirs{$show}{has_recordings} = 1; } #print Dumper(\%tv_show_dirs); # For each season directory that doesn't have recordings, remove it foreach $show (keys %tv_show_dirs) { foreach $season (keys %{$tv_show_dirs{$show}}) { if($season eq "has_recordings") { next; } if ($tv_show_dirs{$show}{$season}{has_recordings} == 0) { print "-> Removing season dir '$show/$season'\n"; system("rm -rf \"$tv_src_dir/$show/$season/\""); } } } # For each show directory that doesn't have any recordings under it, remove it foreach $show (keys %tv_show_dirs) { my $has_recordings = 0; if($show eq ".grab") { next; } foreach $season (keys %{$tv_show_dirs{$show}}) { if ($tv_show_dirs{$show}{$season}{has_recordings} == 1) { $has_recordings = 1; } } if($has_recordings == 0) { print "-> Removing show dir '$show'\n"; system("rm -rf \"$tv_src_dir/$show/\""); } }