3

I have a directory reference in my Downloads directory that contains a symbolic link (created with ln -s) to another directory. I get conflicting error message when trying to remove the symlink:

rm returns "Is a directory"

rmdir returns "Not a directory"

This only occurs with cellranger/ (followed by a forward slash) and not with cellranger.

[tom@rlgsw68 cellranger]$ pwd
/home/tom/Downloads/reference

[tom@rlgsw68 cellranger]$ ls -lth
lrwxrwxrwx 1 tom genome 33 Apr  4 14:52 cellranger -> /analysisdata/genomes/cellranger/

[tom@rlgsw68 cellranger]$ rm cellranger/
rm: cannot remove directory `cellranger/': Is a directory

[tom@rlgsw68 cellranger]$ rmdir  cellranger/
rmdir: cellranger/: Not a directory

[tom@rlgsw68 cellranger]$ rm cellranger

Why does neither of these commands to remove the symlink work and why do these conflicting errors occur? What is the recommended way to remove symbolic links without removing the content in the source directory. rm -rf cellranger/ also does not remove the symlink (but does not return an error).

example of terminal session

Information: I'm running a linux server (Debian 9.0). These errors occur with both bash and zsh. Ambiguous names have been removed from the example. I encountered this when a directory included a link to the parent directory in addition to the contents:

/home/tom/Downloads/reference/cellranger/cellranger/ -> /analysisdata/genomes/cellranger/
Tom Kelly
  • 1,458
  • 17
  • 25
  • 1
    Normally, `rm` decides on whether it's deleting a file or a directory based on the `-r` flag, or `lstat`-ing the thing you give it. It may be that `unlink` or `lstat` has paths that check the string you've given it before any other check. If you provide `xyzzy/`, it may assume that's a directory straight away. My advice would be to not add `/` to whatever you're trying to delete :-) – paxdiablo Apr 05 '19 at 02:43
  • `rm -rf cellranger/` does not return an error but fails to remove the symlink of the directory. `rm -rf cellranger` works just like `rm cellranger`. – Tom Kelly Apr 05 '19 at 04:13
  • By definition, `cellranger/` addresses the directory behind the link and `cellranger` the softlink itself. This is true for other tools like `ls` or `stat`. So `rm cellranger` is the right command. – Wiimm Apr 05 '19 at 06:54

1 Answers1

4

By putting a trailing slash you are referring to the directory the symlink points to, no longer the symlink itself. Printing the inode number (the number that a path refers to in the file system) shows the difference between dereferencing the symlink and the directory:

$ cd "$(mktemp --directory)"
$ mkdir a
$ stat --format %i a/
9
$ ln --symbolic a b
$ stat --format %i b
10
$ stat --format %i b/
9

This may be related to the fact that a symlink is never a directory, it is always just a file containing a path.

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • 1
    Thanks, that helps to understand the situation. Could you please explain what the stat command is giving us here (how to interpret it). Is this similar to “l” or “d” at the start of an `ls -l` output. btw, I think there is a mistake in your code (unmatched quotes). I use a zsh configuration that colours files, links and directories differently. – Tom Kelly Apr 05 '19 at 06:44