nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Find out if a symlink is broken or not

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 ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-07-2009, 08:43 PM
raj's Avatar
raj raj is offline
Senior Member
User
 
Join Date: Jun 2005
Location: Hyderabad
OS: Fedora, Debian Linux
Posts: 307
Thanks: 42
Thanked 8 Times in 8 Posts
Rep Power: 6
raj will become famous soon enough raj will become famous soon enough
Default Find out if a symlink is broken or not

My backup script create link to latest tar ball. However, sometime it is deleted by end users. How can I determine whether a symlink is broken or not? If I can find out that, I can create a new symlink
__________________
Raj
Linux rulz.
I have never turned back in my life ; I shall not do so today.. haha

Last edited by raj; 09-07-2009 at 09:39 PM.
Reply With Quote
  #2 (permalink)  
Old 09-07-2009, 09:17 PM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
OS: Suse, RHEL, Vista
Posts: 199
Thanks: 12
Thanked 9 Times in 9 Posts
Rep Power: 7
sweta has a spectacular aura about sweta has a spectacular aura about
Default

Code:
file=/home/sweta/demo/link
if [[ ! -e $file &&  -L $file ]]; then
  echo "$file symlink is  broken!"
fi
__________________
Reply With Quote
The Following User Says Thank You to sweta For This Useful Post:
raj (09-07-2009)
  #3 (permalink)  
Old 09-07-2009, 09:28 PM
raj's Avatar
raj raj is offline
Senior Member
User
 
Join Date: Jun 2005
Location: Hyderabad
OS: Fedora, Debian Linux
Posts: 307
Thanks: 42
Thanked 8 Times in 8 Posts
Rep Power: 6
raj will become famous soon enough raj will become famous soon enough
Default

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
Reply With Quote
  #4 (permalink)  
Old 09-07-2009, 09:47 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Hey Raj

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
Or

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
__________________
Have a look at what I have been working on
http://www.shellasaurus.com

Last edited by jaysunn; 09-07-2009 at 09:50 PM.
Reply With Quote
The Following User Says Thank You to jaysunn For This Useful Post:
raj (10-07-2009)
  #5 (permalink)  
Old 10-07-2009, 01:29 AM
raj's Avatar
raj raj is offline
Senior Member
User
 
Join Date: Jun 2005
Location: Hyderabad
OS: Fedora, Debian Linux
Posts: 307
Thanks: 42
Thanked 8 Times in 8 Posts
Rep Power: 6
raj will become famous soon enough raj will become famous soon enough
Default

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
Reply With Quote
Reply

Tags
linux , ln command , shell , symlink


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

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


All times are GMT +5.5. The time now is 09:34 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38