wting
wting

Reputation: 910

bash: After testing mtime by following a symlink, I need to delete the symlink itself and not the target file

Right now I have a script that creates symlinks to anything newer than 2 weeks in the public folders into another folder. However, I can't find any good way of getting rid of the stale symlinks individually as opposed to wiping everything out. I need to test the symlink target mtime and if it's older than 2 weeks, delete the symlink itself and not the linked file.

#!/bin/bash

source="/media/public/"
dest="/pool/new/"

if [[ ! -d $dest ]]; then
    exit 1
fi

if [ `hostname` == "punk" ] && [ `uname -o` == "GNU/Linux" ]; then
    #rm -f $dest/*
    find -L $dest -mtime 14 -type f -exec echo "delete symlink: " {} \;
    find -L $source -mtime -14 -type f -exec ln -s -t $dest {} \;
fi

Right now the first find command will delete the target as opposed to the symlink.

Upvotes: 2

Views: 391

Answers (1)

Roman Cheplyaka
Roman Cheplyaka

Reputation: 38758

Use simply

-exec rm {} +

rm will delete the link itself, not the target.

Upvotes: 1

Related Questions