简体   繁体   中英

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.

Use simply

-exec rm {} +

rm will delete the link itself, not the target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM