Remove directory tree without following symlinks

Random832 random832 at fastmail.com
Sat Apr 23 17:04:29 EDT 2016


On Sat, Apr 23, 2016, at 06:24, Steven D'Aprano wrote:
> "rm -r" gives me a NameError when I run it in my Python script :-)
> 
> But seriously, where is that documented? I've read the man page for
> rm, and it doesn't say anything about treatment of symlinks, nor is
> there an option to follow/not follow symlinks. So I never trust rm -r
> unless I know what I'm deleting.

The Unix Standard says "For each entry contained in file, other than dot
or dot-dot, the four steps listed here (1 to 4) shall be taken with the
entry as if it were a file operand. The rm utility shall not traverse
directories by following symbolic links into other parts of the
hierarchy, but shall remove the links themselves." and "The rm utility
removes symbolic links themselves, not the files they refer to, as a
consequence of the dependence on the unlink() functionality, per the
DESCRIPTION. When removing hierarchies with -r or -R, the prohibition on
following symbolic links has to be made explicit."

OSX (and I assume other BSDs) says "The rm utility removes symbolic
links, not the files referenced by the links."

I don't know why GNU rm's documentation doesn't say anything about its
treatment of symlinks - maybe it never occurred to anyone at GNU that
someone might think it would do anything else.

(I'd be less inclined to trust windows' treatment of symlinks,
junctions, and other reparse points without doing some experiments)



More information about the Python-list mailing list