7

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)                           = ?
3
  • What type of filesystem is mounted? On a local system you would have created a link my_link -> my_link inside of my_dir.
    – Kyle Smith
    Mar 20, 2012 at 21:28
  • @KyleSmith my_link is on a cifs drive, and my_dir is on a local drive.
    – ben
    Mar 20, 2012 at 21:31
  • What OS are you using? What type of server is providing the CIFS share (i.e. Samba, Windows, ...)? Mar 21, 2012 at 9:48

6 Answers 6

10

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

1
  • I don't have a trailing slash in my rm (and autocomplete doesn't even add it for me)
    – ben
    Mar 20, 2012 at 21:19
5

Try to do:

rm -rf my_link

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

2
  • I get the same error when I add -r, and -rf just suppresses the error and doesn't delete the link
    – ben
    Mar 20, 2012 at 21:20
  • How did you delete my_dir? When I repeat what you did my_link ends up in my_dir and you can't delete JUST my_dir unless it's empty.
    – aseq
    Mar 20, 2012 at 21:22
2

Have you tried rm -rf my_link?

3
  • I get the same error when I add -r, and -rf just suppresses the error and doesn't delete the link
    – ben
    Mar 20, 2012 at 21:20
  • have you tried using the unlink command? If that doesn't work, make a link using the same name as this link to a folder, then use ulink. Mar 20, 2012 at 21:28
  • Assuming you meant ulink, I get the same error as rm. I tried ln -s other/ my_link and got: "ln: accessing `my_link': Not a directory"
    – ben
    Mar 20, 2012 at 21:34
2

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.)

2
  • unlink gave the error: cannot unlink `test_dir': Not a directory. And I've added the straces' output to the question.
    – ben
    Mar 21, 2012 at 15:02
  • Very strange: IMHO this means that the path contains at some point something which is not a directory as is touch h; unlink h/o. Did you try to specify the absolute path to rm / unlink? (I cannot explain things, it's just a try.) Mar 21, 2012 at 15:49
1

I managed to solve this, even though I don't fully understand how or what really went wrong.

Looking at Mystery files, "not a directory", I found someone with a similar problem. They were able delete the "mystery file" by ssh'ing into the machine that was doing the sharing and delete the file from there.

I don't have ssh privileges to that machine, but I can FTP. And, amazingly, I was able to delete the link.

I guess the problem was a combination of Samba and bad ln calls. Thanks for all your help getting this sorted.

1

I have the same symptom from a CentOS client on a CIFS mounted resource.

For me, the problem is related to whether the target of the symlink exists at the point in time where you try to remove the symlink. If the target exists, it works. If the target does not exist, removing the symlink fails.

$ touch a
$ ln -s a b
$ ls -l
total 0
-rw-rw-r--. 1 andi andi 0 Jan  6 14:42 a
lrwxrwxrwx. 1 andi andi 1 Jan  6 14:42 b -> a

# Remove link target target and then try to remove link:
$ rm a
$ ls -l
total 0
lrwxrwxrwx. 1 andi andi 1 Jan  6 14:42 b -> a
# With colors on, the b and a are now marked red
$ rm b
rm: cannot remove ‘b’: No such file or directory
$ ls -l
total 0
lrwxrwxrwx. 1 andi andi 1 Jan  6 14:42 b -> a
# With colors on, the b and a are now marked red

# Reestablish link target target and then try to remove link:
$ touch a
$ ls -l
total 0
-rw-rw-r--. 1 andi andi 0 Jan  6 14:44 a
lrwxrwxrwx. 1 andi andi 1 Jan  6 14:42 b -> a
$ rm b
$ ls -l
total 0
-rw-rw-r--. 1 andi andi 0 Jan  6 14:44 a

The same test when performed on a local file system, works fine (i.e. the broken symlink can successfully be removed).

My CIFS client is a CentOS 7, using the cifs-utils package. That CentOS is a guest on VirtualBox on a Windows 7 host, but that should not matter because the network device is virtualized in this scenario, and not the file system or storage device.

My CIFS server is a Synology NAS box with DSM 5.2, with symbolic link support enabled in its CIFS/SMB options.

-> Can other people confirm this behavior? -> Any explanation?

Andy

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .