Posted on

Our family is big on watching NBC Nightly News with Lester Holt, each night. My Plex server records this news program, each day/week. This script allows you to keep only the most recent episode of each broadcast (so that your server isn’t filling up with old episodes. The original script went through and deleted any file older than 10 hours. Then I realized that some programs, like Meet the Press, only come on once a week – but the original script may delete the file before all of us got a chance to watch it. The newer script allows you to keep a certain number of the latest episodes of each program on the server. (I have never gotten the Plex DVR settings to properly work for this functionality, so I wrote my own script).

My recommendation is that you set up a cron job to automatically run the script on a schedule you find appropriate.

This software is free for you to use and licensed with the MIT License. If you do use it, please drop by my GitHub, and drop the Plex-Scripts Repository a star. Any suggestions or bugs? Create a new Issue on GitHub.

# This is a script which is for retention of video files on a Plex Server (DVR)
# The sample use case is retaining only the most recent news program (i.e. yesterday's news)

# The original script was designed to find all videos older than 10 hours and delete them. This would
# run daily at 6:30pm by a cron job. Essentially this was "delete all episodes older than 10 hours"

# The new script is designed to save the most recent video file. This allows for weekly programs with
# episodes older than 1 day, to be retained. Essentially this becomes "keep latest 1 episode" 

# --------------------------------------------------------------------------------------------------
# MIT License

# Copyright (c) 2020 Pranav Mishra
# Online repository at https://github.com/pranavmishra90

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# --------------------------------------------------------------------------------------------------

# It is recommended to run this as a cron job daily

# Uncomment following line if you need the script executed as bash
#!/bin/bash


#News Directory - follwing the 'cd' command, enter the parent directory which you would like the script to run on
cd /volume1/Plex-Media/News

shopt -s globstar
for dir in **/; do
	#The script will enter into each folder, search each subdirectory, with depth '1'. 
	#This means that your folder structure should be:
	# /News
	# /News/Show-1/Season X
	# /News/Show-1/Season Y

	# /News/Show-2/Season X
	# /News/Show-2/Season Y

	# /News/Show-3/Season X
	# /News/Show-3/Season Y


	# The script will enter each season folder, find the latest episode, and delete all of the others
	# It will only find .ts containered files (which are recorded by the DVR)
   	find "$dir" -maxdepth 1 -type f -name '*.ts' -printf '%%T@ %%p\0' |
	sort -rnz |

	# The value of the number below should be n+1, where 'n' is the number of files you want
	# The default value is 2, which means only the most recent file will be kept

	sed -nz '2,$s/[^ ]* //p' 
	done | xargs -0 rm --


#------------------------------------------------------------------
# !!Older script!! - Depracated
# Deletes all .ts and .mp4 files older than 10 hours. 
#------------------------------------------------------------------

#ls *.ts -t | tail -n +2 | xargs rm --

#find . -type f -name "*.mp4" -o -name "*.ts" -mmin +600
#find . -type f -name "*.mp4" -o -name "*.ts" -mmin +600 -delete