Question

I am trying to remove an error from appearing on the terminal (it annoys me as it is not one that I need to be worried about).

I have the following code which will check for a broken symbolic link, and if the link is broken it will delete the link:

find /usr/lib/libdb.so -xtype l -delete

How do I change this to a iIF statement?

if [ broken link ] then;
    delete file
else
    do nothing
fi

Could anyone shed any light on this for me please?

Was it helpful?

Solution 2

To follow your approach, you could try this command:

if [ "`find /usr/lib/libdb.so -type l -xtype l`" != "" ]; then
    echo delete file
else
    echo do nothing
fi

or, more concisely:

find /usr/lib/libdb.so -type l -xtype l -print -delete

OTHER TIPS

You can use this find:

find /usr/lib/libdb.so -type l -not -exec test -e '{}' \; -print -delete

-not -test -e will detect only broken files(links) and delete them after printing.

Not tested but give it a try

FILES=`find /usr/lib/libdb.so | grep -v '\.disabled$' | sort`

for F in $FILES; do
    if [ -L $F ]; then
        if readlink -q $F >/dev/null ; then
            delete file
        else
            DO NOTHING
        fi
    fi
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top