1

I have tvheadend running on my RPi, and although it stores recordings locally, when the disk gets to 80% full it moves them over to an SSHfs filestore and symlinks to the new location.

I'm looking for a way, in a shell script, to delete the remote files when the local symlink has been deleted (e.g. I delete a recording through Kodi), probably limiting this to .ts files.

2 Answers 2

2

Making several assumptions:

  • The remote filename is the same as the local filename/symlink
  • The local files are all in a single directory
  • The remote files are all in a single directory

You can then list remote files and delete any for which there is no local file or symlink. Something like this could work

#!/bin/bash
#
rmt=/path/to/sshfs/storage
lcl=/path/to/local/storage

for itempath in "$rmt"/*.ts
do
    itemfile="${itempath/*\/}"
    if test ! -h "$lcl/$itemfile"
    then
        echo "Removing remote $itemfile with no local symlink" >&2
        rm -f "$itempath"
    fi
done

If you just want to delete all local dangling symlinks in a particular directory you can simplify the code somewhat:

#!/bin/bash
#
lcl=/path/to/local/storage

for item in "$lcl"/*.ts
do
    if test -h "$item" -a ! -e "$item"
    then
        echo "Removing dangling symlink $item" >&2
        rm -f "$item"
    fi
done
2
  • Thank you, works perfectly, however I replaced 'rm -f "$lcl/$itemfile"' with 'rm -f "$rmt/$itemfile"'
    – deejay31
    Feb 22, 2017 at 21:34
  • @deejay31 ah yes, my mistake. I'll fix up my code later. Thanks Feb 22, 2017 at 21:36
0

If I have understood the question correctly, this will do:

test -h <symlink> || rm <remote file>

The code tests if exists and is actually a symlink. If not, will be deleted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .