On 2026-11-16 14:30, Pádraig Brady wrote:
After searching a bit more, I should add that the FreeBSD kernel
handles this transparently.
By "transparently" I assume you mean that on FreeBSD, "rm -r s/" ends by calling rmdir("s/") which removes
the empty directory that the symlink s points to. In contrast, GNU/Linux "rm -r s/" ends by calling unlinkat(AT_FDCWD,
"s/", AT_REMOVEDIR), the equivalent of rmdir("s/"), and this fails with ENOTDIR when s is a symlink to an empty (or
nonempty) directory.
As I understand it, the FreeBSD rmdir syscall conforms to a strict reading of
POSIX, whereas the Linux unlinkat/rmdir syscall does not.
Perhaps something along the lines of:
if (errno == ENOTDIR) {
char *resolved = canonicalize_filename_mode (ent->fts_path,
CAN_EXISTING);
if (resolved && SAME_INODE (lstat(resolved), fts_statp)))
unlinkat (AT_FDCWD, resolved, flag);
}
I'm mildly inclined to stick to the existing behavior, as any such workaround
will introduce a race. My guess is that the Linux kernel folks intentionally
deviated from POSIX in this area, and it's not clear that it's worthwhile to
try to work around this particular deviation.
That being said, we should at least document the situation (and then change the
documentation if we change the code later...). I installed the attached to
document better what coreutils currently does. This affects the documentation
for rm and for rmdir, and updates the already-existing documentation for mv in
this area.
If we do change rm -r's behavior here, I assume we should make similar changes
to rmdir and to mv.
From fbc1bbbec18d062e1fffc201e6bfee0cabbd8a37 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Mon, 16 Nov 2026 19:26:18 -0800
Subject: [PATCH] doc: document "rm -r s/" on symlink s (bug#82259)
---
doc/coreutils.texi | 32 +++++++++++++++++++++++++-------
1 file changed, 25 insertions(+), 7 deletions(-)
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 2905c1d75..98b25fc50 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -10019,13 +10019,12 @@ mv invocation
own the file, or have write permission on its directory.) If the
response is not affirmative, the file is skipped.
-Avoid specifying a source name with a trailing slash,
-when it might be a symlink to a directory.
-Otherwise, @command{mv} may do something very surprising, since
-its behavior depends on the underlying rename system call.
-On a system with a modern Linux-based kernel, it fails with
-@code{errno=ENOTDIR}@.
-However, on other systems (at least FreeBSD 6.1 and Solaris 10) it silently
+Avoid specifying a source name with a trailing slash
+when the unslashed name might be that of a symbolic link to a directory.
+Otherwise, @command{mv} may do something surprising,
+since its behavior depends on the platform.
+On a Linux-based kernels @command{mv} fails with a diagnostic;
+on BSDish kernels it silently
renames not the symlink but rather the directory referenced by the symlink.
@xref{Trailing slashes}.
@@ -10246,6 +10245,16 @@ rm invocation
@cindex directories, removing (recursively)
Remove the listed directories and their contents recursively.
+When using this option, avoid specifying a file name with a trailing slash
+when the unslashed name might be that of a symbolic link to a directory.
+Otherwise, @command{rm} may do something surprising
+after it removes all files under the directory,
+since the behavior depends on the platform.
+On a Linux-based kernel, @command{rm} fails with a diagnostic;
+on BSDish kernels it silently removes not the symlink but rather the
+directory referenced by the symlink.
+@xref{Trailing slashes}.
+
@optItem{rm,-v,}
@optItemx{rm,--verbose,}
Print the name of each file before removing it.
@@ -11155,6 +11164,15 @@ rmdir invocation
If any @var{directory} argument does not refer to an existing empty
directory, it is an error.
+Avoid specifying a file name with a trailing slash
+when the unslashed name might be that of a symbolic link to a directory.
+Otherwise, @command{rmdir} may do something surprising,
+since the behavior depends on the platform.
+On a Linux-based kernel, @command{rmdir} fails with a diagnostic;
+on BSDish kernels it silently removes not the symlink but rather the
+directory referenced by the symlink.
+@xref{Trailing slashes}.
+
The program accepts the following options. Also see @ref{Common options}.
@table @samp
--
2.56.1