Register
It is currently Tue Sep 30, 2014 3:48 am

Find broken symlinks


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sun Nov 05, 2006 7:42 am   

Joined: Wed Sep 06, 2006 12:19 pm
Posts: 54
Location: Covington, WA
Here's an simple script which will find any broken symlinks, log the results, and attach a "*.broken" extension to them.........It's up to you what you want done with them, like deleting them or relinking them to the correct location.......

This scripts also demonstrates how a named pipe (FIFO) can be used effectively....Instead of saving the 'find' results to a tmp file or to a variable, the use of a named pipe in the background (asynchronously) allows the script to continue without having to wait for the completion of the 'find' search before starting the rest of the tasks....which, in this case, is to log anything found and rename them while still searching for more......

Also, to make this work in the background without having the unwanted notices of the background job number and termination printed to the screen, the search being dumped to the FIFO is place in a subshell using parentheses....

Code:
#!/bin/bash
################################################################################
##  linkcheck - Written by CTWaley
##
## Find broken symlinks, log the results and attach a '*.broken' extension to
## the name.  Demonstrates the use of a named pipe (FIFO).
##
## This is freely given by the author to the Public Domain.
################################################################################

FIFO="/tmp/pipe.${RANDOM}.${RANDOM}.${RANDOM}.$$"
LOG="${HOME}/broken-links.log"

## Cleanup on exit:
trap "rm -f ${FIFO}" EXIT
trap "echo '---> Interrupted.'; exit 1" HUP INT QUIT KILL TERM

## Recursively check the directory for symlinks, testing for any new broken
## links, log the results if any are found and attach a '*.broken' extension
## to it (if it doesn't already have one).
test_link(){  ## SYNTAX:  test_link <directory>
     ## Find broken links and save results to the FIFO in the background:
    (sed -rn 's/:[ ]+broken symbolic.+$/.broken/p' \
        <(find "${1}" -noleaf -xdev -mindepth 1 ! -name '*.broken' -type l -print0 |
        xargs -r0 file) >${FIFO} &)
     ## Read the FIFO and append to log file while adding the '*.broken' extension:
    while read;do  echo ${REPLY} >> ${LOG}; mv ${REPLY%.broken} ${REPLY}; done  <"${FIFO}"
}

###
### MAIN
###
## Create the pipe:
mkfifo -m700 ${FIFO}

## If no args are passed to the script search the current directory.
if [ $# -eq 0 ]; then
    test_link ${PWD}
else
     ## Test each arg that was passed to the script to see if it is a valid
     ## directory.  If not, silently skip it:
    for DIR; do  test -d ${DIR}  &&  test_link ${DIR};  done
fi

## Remove any blank lines from the log file:
sed -i '/^$/d' ${LOG}

## If the log file is empty, delete it:
if [ ! -s ${LOG} ];then
    rm -f ${LOG}
else
    echo "Check log file, ${LOG}, for list of broken symlinks.";  fi

exit 0


:-)
---thegeekster


Top
 Profile  
 PostPosted: Sun Nov 05, 2006 3:07 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 667
Location: Des Moines, Iowa
I like it :D


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP