bugfindutils - Bugs: bug #46305, Doing "find -L . -type d...


bug #46305: Doing "find -L . -type d -delete" fails on symlinks to directories.

Submitted by:  C <pfudd>
Submitted on:  Tue 27 Oct 2015 01:23:12 AM UTC  
Category: findSeverity: 4 - Important
Item Group: Wrong resultStatus: Wont Fix
Privacy: PublicAssigned to: James Youngman <jay>
Originator Name: Open/Closed: Open
Release: 4.4.2Fixed Release: None

Add a New Comment(Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


Sun 01 Nov 2015 09:23:19 PM UTC, comment #1:

> The '-L' option makes '-type d' detect symlinks to directories, but '-delete' doesn't treat them as such.

I don't think that's what is happening. Use of -delete automatically turns on -depth. This means that the filesystem is visited in postorder. For the command line " find -L . -type d -delete" this means that the -delete action takes place for children and some siblings before it takes place for some item. In the case of symbolic links this will mean that, at least some of the time, the thing the link points to will have been deleted before the link is examined by find.

That means that, at the time bar (in your example) is examined, "-type d" is no longer true for it. It's a broken symlink instead (-type l would match it) and so it is not deleted since -type d is false.

I don't think this problem can be "solved" by special-casing directories, as I'd imagine that analogous things apply to link counts or (sometimes) directory sizes.

I think then that find is working as designed and that changing this aspect of the design to change this behaviour would likely give rise to semantics that are both inconsistent and much harder to explain (because of the special-casing).

I'd recommend simply avoiding the use of -L with -delete (and in any case that combination risks causing the deletion of unexpected parts of the tree where third parties can make symbolic links to / for example).

Please feel free to follow up if you disagree or you can see a simple solution. In the meantime I will work on some documentation updates to try to make things clearer.

James Youngman <jay>
Project AdministratorIn charge of this item.
Tue 27 Oct 2015 01:23:12 AM UTC, original submission:

The '-L' option makes '-type d' detect symlinks to directories, but '-delete' doesn't treat them as such.

$ mkdir /tmp/foo
$ cd /tmp/foo
$ mkdir baz
$ ln -s baz bar
$ find -L . -type d
$ find -L . -type d -delete
find: cannot delete `./bar': Not a directory
$ ls -l
total 0
lrwxrwxrwx 1 root root 3 Oct 26 18:12 bar -> baz

I'd expect --delete to delete whatever it found.

Note: if 'bar' is the directory and 'baz' is the symlink, then no error message is generated, but baz remains as a broken symlink, since the directory was deleted before the symlink was found (assuming sorted directory traversal).

Also, http://ftp.gnu.org/gnu/findutils/ indicates that 4.4.2 is the latest version (from 2009), but this bug-reporting page indicates that 4.5.14 is the latest version. Please link to the new download page on https://savannah.gnu.org/projects/findutils/, or expect bug reports about ancient versions of the code. :-)


C <pfudd>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):

Attached Files
file #35360:  sv-43605-base.sh added by jay (2KiB - application/x-shellscript)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -unavailable- added by jay (Updated the item)
  • -unavailable- added by pfudd (Submitted the item)

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follow 4 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sun 01 Nov 2015 09:24:52 PM UTCjayAttached File-=>Added sv-43605-base.sh, #35360
    Sun 01 Nov 2015 09:23:19 PM UTCjayStatusNone=>Wont Fix
    Sun 01 Nov 2015 01:30:05 PM UTCjaySeverity3 - Normal=>4 - Important
      Assigned toNone=>jay

    Back to the top

    Powered by Savane 3.1-cleanup1