Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Sep 3 '10 at 1:33

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

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

You can use realpath like this:

rm $(realpath A)

Setting up an example:

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

Showing that realpath does what you want:

$ realpath A

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:


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

if ! type readlink >/dev/null 2>&1; then
    echo " cannot find readlink program" 1>&2
    exit 1

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


$ stat -c "%N" *
`A' -> `B'
`B' -> `C'
$ ~/bin/
Usage: <file>...
$ ~/bin/ 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
I think it's spelled "because", not "cuz" ;) – Wuffers Nov 6 '10 at 0:26
edited, cuz "because" is better ;-) – aularon Jan 17 '14 at 12:20

Your Answer


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.