Can't delete symbolic link

I accidentally reversed the parameters of my ln and called:

ln -s my_link my_dir

where my_dir already existed and my_link did not. I deleted my_dir, but am unable to delete my_link.

If I ls -l my_link's parent directory I get 1 of 2 different responses, either:

l????????? ? ?       ?       ?            ? my_link

or:

lrwxrwxrwx 1 username username 8 Mar 20 16:34 my_link

If I call rm my_link I get:

rm: cannot remove `my_link': Not a directory

And, I'm not sure if it's relevant, but my_link resides on a network mounted drive.

Does anyone know what I've done here, and how to remove the broken link?

Edit: I looked through my bash_history and found the commands I ran. It looks like I didn't actually delete my_dir, it simply ceased to be. And I ran several versions of the wrong ln command.

Here's the unedited list of commands I ran before getting the error deleting:

ln -s test_dir ~/netdrive/test_dir/
ln -s test_dir ~/netdrive/test_dir
ln -s test_dir/ ~/netdrive/test_dir/

You'll notice that I've done away with my_link and my_dir. The actual names are both test_dir, and this has probably contributed to some of my confusion (a mistake I hope to never make again).

So now there is no longer a directory ~/test_dir, there is a directory ~/netdrive/test_dir/ and there is the broken link ~/netdrive/test_dir/test_dir

It's that last one that I'm trying to delete. I hope that clarifies rather than muddies the water more.

Edit 2:

My OS is CentOS and I'm pretty sure the network drive is a Samba.

Here's the last couple of lines of output from strace -f -v unlink test_dir:

unlink("test_dir")                      = -1 ENOTDIR (Not a directory)
open("/usr/share/locale/locale.alias", O_RDONLY) = 3
fstat(3, {st_dev=makedev(8, 3), st_ino=1573552, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=2512, st_atime=2012/03/21-02:19:02, st_mtime=2012/03/15-13:42:39, st_ctime=2012/03/16-05:31:49}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc843b2f000
read(3, "# Locale name alias data base.\n#"..., 4096) = 2512
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7fc843b2f000, 4096)            = 0
open("/usr/share/locale/en_US.UTF-8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US.utf8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.UTF-8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.utf8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/coreutils.mo", O_RDONLY) = 3
fstat(3, {st_dev=makedev(8, 3), st_ino=1575882, st_mode=S_IFREG|0644, st_nlink=2, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=435, st_atime=2012/03/20-11:53:41, st_mtime=2011/12/07-16:51:00, st_ctime=2011/12/21-05:34:01}) = 0
mmap(NULL, 435, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fc843b2f000
close(3)                                = 0
write(2, "unlink: ", 8unlink: )                 = 8
write(2, "cannot unlink `test_dir'", 24cannot unlink `test_dir') = 24
open("/usr/share/locale/en_US.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US.utf8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.utf8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, ": Not a directory", 17: Not a directory)       = 17
write(2, "\n", 1
)                       = 1
close(1)                                = 0
close(2)                                = 0
exit_group(1)                           = ?

And the last couple of lines from strace -f -v rm test_dir:

newfstatat(AT_FDCWD, "test_dir", 0x7fff71464640, AT_SYMLINK_NOFOLLOW) = -1 ENOTDIR (Not a directory)
open("/usr/share/locale/locale.alias", O_RDONLY) = 3
fstat(3, {st_dev=makedev(8, 3), st_ino=1573552, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=2512, st_atime=2012/03/21-02:19:02, st_mtime=2012/03/15-13:42:39, st_ctime=2012/03/16-05:31:49}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f039e56b000
read(3, "# Locale name alias data base.\n#"..., 4096) = 2512
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f039e56b000, 4096)            = 0
open("/usr/share/locale/en_US.UTF-8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US.utf8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.UTF-8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.utf8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/coreutils.mo", O_RDONLY) = 3
fstat(3, {st_dev=makedev(8, 3), st_ino=1575882, st_mode=S_IFREG|0644, st_nlink=2, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=435, st_atime=2012/03/20-11:53:41, st_mtime=2011/12/07-16:51:00, st_ctime=2011/12/21-05:34:01}) = 0
mmap(NULL, 435, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f039e56b000
close(3)                                = 0
write(2, "rm: ", 4rm: )                     = 4
write(2, "cannot remove `test_dir'", 24cannot remove `test_dir') = 24
open("/usr/share/locale/en_US.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US.utf8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.utf8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, ": Not a directory", 17: Not a directory)       = 17
write(2, "\n", 1
)                       = 1
close(0)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(1)                           = ?

Solution 1:

Make sure you're not including a trailing /

i.e. rm my_link NOT rm my_link/

The trailing / denotes that you would like to delete the contents beyond the symlink

Solution 2:

Try to do:

rm -rf my_link

I tested it on a local filesystem and on a network filesystem (NFS) and it worked.

Solution 3:

Have you tried rm -rf my_link?

Solution 4:

Please try

unlink test_dir

If this does not help, please add the output of

strace -f -v unlink test_dir

and

strace -f -v rm test_dir

(Only the last few lines - starting from 'unlink' or 'newfstatat' are needed.)