This is for my media server:

I have a folder containing all of my TV Shows, and for some shows, I keep then indefinitely, while others I'd like to clean out the media files as they hit a 90 day age.

So I'd like to populate a list of the shows, by their path, that I want to be purged after 90 days in a TXT file and a scheduled script that will run thru that list daily.

This way I can add or remove shows from the TXT list, and then know that anything on that list I have 90 days to watch before those show's episodes are deleted.

Of course, I'd like to clear the video files and not others, like the season folders, artwork etc. So the typical list of extensions - mkv, mp4, mpg, mpeg, avi, xvid, ts will go, but not the folder structure.

I've found plenty of how-to's for purging files older than X days, but not something this specific.

When I had my media files on a Windows PC, I ran a simple program called autodelete where you specified the folders and the days and the extensions and it took care of things, but since the move to Ubuntu, I haven't found the same.

Any help appreciated.

  • Since this exact script is not found easily with a google search, I think you might want to start looking into adapting scripts that are very near to what you want, for example this script comes very close. – Robert Riedl Feb 5 at 18:16

There's no need for any additional program or even a script, find can do this with a single call.

I assume you have a directory tree like this:

.
├── delete
├── GoT
│   ├── artwork.png
│   ├── S10E05_Everybody_dies.mkv
│   └── stream.ts
├── TBBT
│   └── Season_01
│       ├── file.avi
│       └── notes
└── The_Wire
    ├── current.mp4
    └── wallpaper.jpg

The content of the file delete looks like this:

/full/path/to/GoT
/full/path/to/TBBT

This find command will search every of those directories for files with the specified extensions and an age of more than 89 days:

find $(</path/to/delete) -type f \( -name "*.mkv" -o -name "*.mp4" -o -name "*.mpg" -o -name "*.mpeg" -o -name "*.avi" -o -name "*.xvid" -o -name "*.ts" \) -mtime +89

My example's output is:

/full/path/to/GoT/stream.ts
/full/path/to/GoT/S10E05_Everybody_dies.mkv
/full/path/to/TBBT/Season_01/file.avi

Use this to test the command. If it does what you want just add -delete to the end of the command to perform the deletion:

find $(</path/to/delete) -type f \( -name "*.mkv" -o -name "*.mp4" -o -name "*.mpg" -o -name "*.mpeg" -o -name "*.avi" -o -name "*.xvid" -o -name "*.ts" \) -mtime +89 -delete

You can run this last command periodically e.g. using a cronjob (but set SHELL=/bin/bash in the crontab first, cron uses dash by default!) – the keyword @daily could be of use:

@daily find $(</path/to/delete) …

A note on spaces in paths: I really recommend to get rid of spaces in directory and file names in general, but if you really need to have spaces in the paths you might want to set IFS=$'\n' first to allow only the newline character as a delimiter for arguments.

  • You'd probably need to set SHELL=/bin/bash in crontab, $(<...) won't work in dash. – muru Feb 6 at 8:43
  • 1
    Wow! Thanks very much for the quick response. I'll give it a test run and see how things go. – DGC Feb 6 at 19:25

Okay, here's my feedback.

Yes, the script works, but with a big warning.

If the "delete" file is not found, it will run successfully, anyway, but against any and all files and applies to wherever you happen to be sitting in the command line.

  • Is there a way to put a check for the file's existence else end?

I tried putting quotes in the delete file, to handle the spacing, but it did not take this well. So I performed your suggestion.

Here's the exact command, as-run:

IFS=$'\n' && find $(</opt/deletelist) -type f \( -name "*.mkv" -o -name "*.mp4" -o -name "*.mpg" -o -name "*.mpeg" -o -name "*.avi" -o -name "*.xvid" -o -name "*.ts" \) -mtime +89

I did get the results I expected, so it does appear to work.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.