Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

currently i am wanted to clean up my proj area but the problem with rm i am facing is some copy remain in disk
with linked i am meaning soft link

A linked to B 
 B linked to C

C which is on other directory A & B are on same folder

know when i run

rm -rf A 

it removes only A & B but the C remain on the disk how i can remove C from the disk.. using the the same command..

share|improve this question

migrated from stackoverflow.com Sep 3 '10 at 1:33

This question came from our site for professional and enthusiast programmers.

1  
rm -rf A only removes A, not B. –  Mikel Feb 12 '11 at 23:55

2 Answers 2

up vote 1 down vote accepted

You can use realpath like this:

rm $(realpath A)

Setting up an example:

$ cd $(mktemp -d)
$ pwd
/tmp/tmp.QwSuHKmWwE
$ touch C
$ ln -s C B
$ ln -s B A
$ stat -c "%N" *
`A' -> `B'
`B' -> `C'
`C'

Showing that realpath does what you want:

$ realpath A
/tmp/tmp.QwSuHKmWwE/C

So running rm $(realpath A) is like running rm C.

$ rm $(realpath A)
$ stat -c "%N" *
`A' -> `B'
`B' -> `C'

Or did you want to remove all three files?

In that case, I think you'll have to write a script.

Here's something that should do the job:

#!/bin/bash

if test $# -eq 0; then
    echo "Usage: dellinks.sh <file>..." 1>&2
    exit 2
fi

if ! type readlink >/dev/null 2>&1; then
    echo "dellinks.sh: cannot find readlink program" 1>&2
    exit 1
fi

for file in "$@"; do
    while test -L "$file"; do
        target="$(readlink "$file")"
        rm "$file"
        file="$target"
    done
    if test -e "$file"; then
        rm "$file"
    fi
done

Example:

$ stat -c "%N" *
`A' -> `B'
`B' -> `C'
`C'
$ ~/bin/dellinks.sh
Usage: dellinks.sh <file>...
$ ~/bin/dellinks.sh A
$ stat -c "%N" *
stat: cannot stat `*': No such file or directory
share|improve this answer

You can try

rm -fr `readlink B`

but this will work with B since it will remove it's target C, but not with A because then it will only remove B (A's target).

but that can be done easily via a script that will recursively follow links till they get a non link, then pass it to rm -fr

share|improve this answer
1  
I think it's spelled "because", not "cuz" ;) –  Wuffers Nov 6 '10 at 0:26
    
edited, cuz "because" is better ;-) –  aularon Jan 17 at 12:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.