2 # Author: Alan J. Pippin
3 # Description: Find and remove TV recording directories that don't contain any recordings in them
13 ####################################################################################################
14 # Configuration parameters
15 my $tv_src_dir = "/tvpool/tv";
16 my $log = "/var/log/organize/remove_empty_tv_dirs.log";
17 my $tv_file_ext = "-iregex \".*\.mov\" -o -iregex \".*\.avi\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mpg\" -o -iregex \".*\.ts\" -o -iregex \".*\.mkv\"";
18 my $find_tv_show_dirs = "find \"$tv_src_dir/\" -maxdepth 1 -cmin +14400 -type d";
19 my $find_tv_season_dirs = "find \"$tv_src_dir/\" -maxdepth 2 -mindepth 2 -cmin +14400 -type d";
20 my $find_tv_shows = "find \"$tv_src_dir/\" -cmin +14400 $tv_file_ext";
22 ####################################################################################################
23 # Find empty TV show directories to remove
27 # Find all of our TV season dirs and store them in a hash
28 foreach $dir (`$find_tv_season_dirs`) {
30 my $season = basename($dir);
31 my $show = basename(dirname($dir));
32 #print "-> Found dir $dir show $show season $season\n";
33 $tv_show_dirs{$show}{$season}{has_recordings} = 0;
35 foreach $dir (`$find_tv_show_dirs`) {
37 my $show = basename($dir);
38 #print "-> Found dir $dir show $show\n";
39 $tv_show_dirs{$show}{has_recordings} = 0;
42 # Find all of our TV show recordings and store them in a hash
43 foreach $file (`$find_tv_shows`) {
45 my $recording = basename($file);
46 my $season = basename(dirname($file));
47 my $show = basename(dirname(dirname($file)));
48 #print "-> Found show $show season $season and recording $recording\n";
49 $tv_show_dirs{$show}{$season}{has_recordings} = 1;
50 $tv_show_dirs{$show}{has_recordings} = 1;
53 #print Dumper(\%tv_show_dirs);
55 # For each season directory that doesn't have recordings, remove it
56 foreach $show (keys %tv_show_dirs) {
57 foreach $season (keys %{$tv_show_dirs{$show}}) {
58 if($season eq "has_recordings") { next; }
59 if ($tv_show_dirs{$show}{$season}{has_recordings} == 0) {
60 print "-> Removing season dir '$show/$season'\n";
61 system("rm -rf \"$tv_src_dir/$show/$season/\"");
66 # For each show directory that doesn't have any recordings under it, remove it
67 foreach $show (keys %tv_show_dirs) {
68 my $has_recordings = 0;
69 if($show eq ".grab") { next; }
70 foreach $season (keys %{$tv_show_dirs{$show}}) {
71 if ($tv_show_dirs{$show}{$season}{has_recordings} == 1) {
75 if($has_recordings == 0) {
76 print "-> Removing show dir '$show'\n";
77 system("rm -rf \"$tv_src_dir/$show/\"");