delete dead links


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete dead links
# 1  
Old 06-19-2009
delete dead links

i am writing small shell script

how can i find dead links
How can delete dead links

Thanx
# 2  
Old 06-19-2009
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. Smilie
# 3  
Old 06-19-2009
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  
Old 06-19-2009
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 09:05 AM..
# 5  
Old 06-19-2009
Thanks.

Quote:
Originally Posted by panyam
Nice explanation Rakesh..
Thanks panyam.
# 6  
Old 06-19-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. AIX

List all the soft links and hard links

Hi I'm logged in as root in an aix box Which command will list all the soft links and hard links present in the server ? (2 Replies)
Discussion started by: newtoaixos
2 Replies

2. Solaris

Hard Links and Soft or Sym links

When loooking at files in a directory using ls, how can I tell if I have a hard link or soft link? (11 Replies)
Discussion started by: Harleyrci
11 Replies

3. Shell Programming and Scripting

Delete and Recreate around 400 Symbolic Links in one Folder

Hello, I recently just switched my phphandler to suPHP on my server in order to increase security a bit. I am working with 2 scripts, one of which is vbulletin forums and the other is custom. The vbulletin forum avatars are all symbolic links to pictures in the other custom script. When I... (2 Replies)
Discussion started by: ambition13
2 Replies

4. What is on Your Mind?

Usenet is dead

On servers i check there seems to be no news at all. (3 Replies)
Discussion started by: Action
3 Replies

5. Shell Programming and Scripting

delete links along with the files

Hi, I am new to unix I am removing some unwanted files from the directory by using the following command find . -name "*" -exec rm -f {} \; But i want the corresponding links also to be removed can one help me Thanks (1 Reply)
Discussion started by: Satyak
1 Replies

6. Shell Programming and Scripting

what is dead.letter ??

Hi all can you please help me what is dead.letter file ? when it is created ? for the first time i have seen this file getting created in my current directory? I am using SunOs. Any IDEA ?? (2 Replies)
Discussion started by: jambesh
2 Replies

7. Solaris

Dead SUN

My SUN V210 refuses to fully boot up. We had a power outage (ie. someone tripped the cord) and thereafter the Sun will not come up, and the OS is not starting. The LED on the front is not lit. Monitor gets no feed, so I plugged in via the management port. The system comes up to: Trap 3e. and... (7 Replies)
Discussion started by: ireeneek
7 Replies
Login or Register to Ask a Question