Solved find dead links and remove them.

this is a function I have in a script I use, as this does not work in FreeBSD, and I've been removing images I know I got dead links. Now I am trying to figure out how to do this in FreeBSD,
Code:
deleteDeadLinks()
{
    echo "Deleting dead Links..."
    #Delete any dead links to files
    [[ -d "$localEterm" ]] &&
    { cd /usr/local/share/Eterm/pix/scale ;
    sudo find  . -xtype l -delete ;}

    [[ -d "$usrEterm" ]] && { cd /usr/share/Eterm/pix/scale ;
        sudo find  . -xtype l -delete ; }
    echo "Done deleting dead links.."
    exitCode=$?

}
Code:
-links n
             True if the file has n links.

     -lname pattern
             Like -name, but the contents of the symbolic link are matched
             instead of the file name.  Note that this only matches broken
             symbolic links if symbolic links are being followed.  This is a
             GNU find extension.

Testing round:
I created two sub-folders, put two images in one, sym linked them into another, deleted one image ran this find on it to try and delete the dead link.

it returns the broken link
Code:
[userx@FreeBSD64 testscripts]$ find -L $(pwd)/link2/ -lname "*.jpg"
/home/userx/testscripts/link2/MacOS-10-14-Day.jpg
Now try to delete it
Code:
[userx@FreeBSD64 testscripts]$ find -L $(pwd)/link2/ -lname "*.jpg" -delete
find: -delete: forbidden when symlinks are followed
what would be the proper syntex to accomplish this?
 
OK - overthinking it, maybe I was.
would this be a safe replacement?
Code:
[userx@FreeBSD64 testscripts]$ find -L $(pwd)/link2/ -lname "*.jpg" -exec rm -v {} \;
/home/userx/testscripts/link2/MacOS-10-14-Day.jpg

I am assuming yes, and just adding this for whomever may need to look up find and delete dead syslink.

Code:
deleteDeadLinks()
{
    echo "Deleting dead Links..."
    #Delete any dead links to files
    [[ -d "$localEterm" ]] &&
    { cd /usr/local/share/Eterm/pix/scale ;
    #sudo find  . -xtype l -delete ;}
    #for FreeBSD
    sudo find -L . \( -lname "*.jpg" -o -lname "*.png" \)  -exec rm -v {} \; ;}

    [[ -d "$usrEterm" ]] && { cd /usr/share/Eterm/pix/scale ;
        #sudo find  . -xtype l -delete ; }
        #for FreeBSD
    sudo find -L . \( -lname "*.jpg" -o -lname "*.png" \)  -exec rm -v {} \; ;}
    echo "Done deleting dead links.."
    exitCode=$?

}
 
Instead of ... -delete I would use ... -print0 | xargs -0 rm
I was kind of wondering if I should use an xargs on it or not. I'll change it to this
Code:
  sudo find -L . \( -lname "*.jpg" -o -lname "*.png" \)  -exec rm -v {} \;

#to that

find -L . \( -lname "*.jpg" -o -lname "*.png" \)  -print0 | xargs -0 sudo rm -v
Tested and it too works.
 
Just out of curiosity, why do you use sudo? That's kind of dangerous. So if you don't really need it, don't use it. And if you do need it, there might be a better way to do it, e.g. by leveraging group permissions.
 
Just out of curiosity, why do you use sudo? That's kind of dangerous. So if you don't really need it, don't use it. And if you do need it, there might be a better way to do it, e.g. by leveraging group permissions.
its going into the system side to do the things it needs to so it needs root privs, this is just a personal laptop, not a server, and there's nothing on here to worry about, personal Info, goverement secrets, etc.., so I do not apply that worry to my scripts.
 
Back
Top