Linux / UNIX Tech Support Forum
This is a discussion on Find out if a symlink is broken or not within the Shell scripting forums, part of the Development/Scripting category; My backup script create link to latest tar ball. However, sometime it is deleted by end users. How can I ...
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
| Sponsored Links | ||
|
|
|
||||
|
Code:
file=/home/sweta/demo/link if [[ ! -e $file && -L $file ]]; then echo "$file symlink is broken!" fi
__________________
|
| The Following User Says Thank You to sweta For This Useful Post: | ||
raj (09-07-2009)
| ||
|
||||
|
thanks but I'm not on bash, I need something which is posix ready and should works across any UNIX version.
__________________
Raj Linux rulz. I have never turned back in my life ; I shall not do so today.. haha |
|
||||
|
Give this a try:
Code:
#/usr/bin/find / -type l ! -execdir test -e '{}' \; -print | nl
/usr/bin/find -x / -type l ! -execdir test -e '{}' \; -ls | nl
# skip /dev directory
/usr/bin/find / -not \( -type d -path '/dev' -prune \) -type l ! -execdir test -e '{}' \; -ls | nl
Code:
# find broken symlinks to non-existing files & directories or another broken symlink
# "Using the -L flag follows symlinks, so the -type l test only returns true if the link can't be followed,
# or is a symlink to another broken symlink."
/usr/bin/find -x -L / -type l -ls | nl
/usr/bin/find -L / -not \( -type d -path '/dev' -prune \) -type l -ls | nl # skip /dev directory
# delete broken symlinks
#/usr/bin/find -x / -type l ! -execdir test -e '{}' \; -delete
#/usr/bin/find / -not \( -type d -path '/dev' -prune \) -type l ! -execdir test -e '{}' \; -delete
#/usr/bin/find -x -L / -type l -delete
#/usr/bin/find -L / -not \( -type d -path '/dev' -prune \) -type l -delete
This should be portable. Tested ON BSD / MAC OSX Jaysunn Last edited by jaysunn; 09-07-2009 at 09:50 PM. |
| The Following User Says Thank You to jaysunn For This Useful Post: | ||
raj (10-07-2009)
| ||
|
||||
|
Thanks for find commands, I will check it out later on my sun box.
__________________
Raj Linux rulz. I have never turned back in my life ; I shall not do so today.. haha |
![]() |
| Tags |
| linux , ln command , shell , symlink |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| DNS broken after Fedora 10 upgrade | woodson2 | CentOS / RHEL / Fedora | 0 | 03-12-2008 08:06 PM |
| DNS broken after Fedora 10 upgrade | woodson2 | Domain Name Server | 0 | 03-12-2008 08:05 PM |
| How to follow symlink on /var/www/html | demuytree | Shell scripting | 0 | 15-10-2008 11:05 PM |