Just another dog on the internet

How to automatically delete a file after playback with mpv

2022.12.06

I often watch local video files using the mpv video player, often invoking it from the command line for easy accessibility. In some cases I would like to delete the played file after playback. Since there is no shortcut in mpv to delete the currently playing file, I created a very basic script to handle this task.

Creating the deletion script

Create a new executable file named rmpv in with the following content:

#!/bin/bash

### __author__ = "joseti.me/about" 
### __version__ = "1.0.1"
### __license__ = "GPLv3"

###
### very basic shell utility script to play files using mpv 
### and ask to delete the provided files afterwards
### requires 'mpv' to be successfully set up on system working
### command syntax: rmpv <files>
###
### does not support switches or flags
###

mpv "$@" && rm -i "$@"

Place the rmpv executable in an appropriate location and reload your shell.

Using the deletion script

Playing a file that you want to delete afterwards is now just a matter of using rmpv instead of the traditional mpv. After playback you will be queried if you want to delete the played file, just press y to confirm, any other key will keep the file as-is. You can also pass multiple files to be played to the script, even shell-based wildcards are possible.

The script is only intended for basic playback, so flags will be passed to mpv but may cause errors during the deletion process, however nothing that a simple shell interrupt wouldn’t be able to handle.