The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Is SCO finally dead? iBot UNIX and Linux RSS News 0 07-18-2008 09:20 AM
what is dead.letter ?? jambesh Shell Programming and Scripting 2 08-30-2006 03:25 AM
dead.letter unisam UNIX for Dummies Questions & Answers 6 03-31-2004 02:33 AM
Dead SUN ireeneek SUN Solaris 7 12-16-2003 08:58 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-19-2009
KiranKumarKarre KiranKumarKarre is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 62
delete dead links

i am writing small shell script

how can i find dead links
How can delete dead links

Thanx
  #2 (permalink)  
Old 06-19-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
suppose you have a b c files in a dir
where c -> a
and b is a hard link to a ...
if you do ls -l
you would see

Code:
total 0
-rw-rw-r--    a
-rw-rw-r--    b
lrwxrwxrwx    c -> a
with some more columns in between...
If you delete a ... there wiill not be any impact on b...but c will become a dead link...
Now you want is, find files like c and delete them...

Code:
$ls -1L 2>/dev/null
b  

gives b because we have deleted a and c is a dead link.

$ls -1
b
c
gives both b and c.
so the difference of the two commands is c...delete it.

Code:
set -A _Array1 `ls -1 | xargs`
set -A _Array2 `ls -1L 2>/dev/null | xargs`

for file in ${_Array1[@]}
do
        _dead=0
        for link in ${_Array2[@]}
        do
                if [[ $file = $link ]]
                then
                        _dead=1
                        break;
                fi
        done
        if [[ $_dead -ne 1 ]]
        then
                echo $file is a dead link.
                #/bin/rm -f $file
        fi
done
I have commented the rm part...first run the program, see you are getting what you want and then run it.
  #3 (permalink)  
Old 06-19-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,042
for FILE in *; do
ls -dL $FILE >/dev/null 2>&1 || echo RM $FILE
done

Or recursively...

for FILE in $(find .); do
ls -dL $FILE >/dev/null 2>&1 || echo RM $FILE
done

replace the echo RM with rm when you're happy!
  #4 (permalink)  
Old 06-19-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Rakesh

Code:
$ls -1L 2>/dev/null
in my case it's returning all files . Is it shell or OS dependent ??..

Last edited by panyam; 06-19-2009 at 08:05 AM..
  #5 (permalink)  
Old 06-19-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
Thanks.

Quote:
Originally Posted by panyam View Post
Nice explanation Rakesh..
Thanks panyam.
  #6 (permalink)  
Old 06-19-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,163
On unix systems a dead link file when viewed with "ls -ladL" has a link count of zero no permissions and is dated the epoch. Dead links in system partitions are best left alone - they can be there for good reason.
To find dead links:

Code:
#!/bin/ksh
find . -type l -print | while read FILENAME
do
        LINK_COUNT=0
        LINK_COUNT=`ls -ladL "${FILENAME}"|awk '{print $2}'`
        if [ ${LINK_COUNT} -eq 0 ]
        then
                ls -ald "${FILENAME}"
                ls -aldL "${FILENAME}"
                echo ""
        fi
done
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 07:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0