Cannot remove symbolic link to directory

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/

Upvotes: 3

Views: 7141

Answers (1)

l0b0
l0b0

Reputation: 58808

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.

Upvotes: 5

Related Questions