As far as I know, deleting a non empty directory could work the same way as deleting an empty directory: by removing the pointer to the directory's metadata there would be no pointers to the items it contained, effectively deleting all its children recursively.

If that's true, then why must directories be empty before being deleted? Is it just a safeguard to prevent erasing many files at once, or a technical limitation of some (possibly ancient) file system?

  • 1
    This assumes that the filesystem will automatically recover the space from those orphaned files, and that may not be the case. Apr 15 '14 at 18:27
  • To @Satanicpuppy's point, there are many implementations of file systems in active use today, and any one of them in production use (NTFS, ext4, etc) are extremely complicated. Given that one of the goals of a modern file system is disaster recovery (ref: journaling), the idea of needing to reclaim space explicitly makes a lot of sense.
    – user22815
    Apr 15 '14 at 18:40
  • @JohnGaughan Agreed. Almost all resource reclamation is more nuanced than people appreciate. Apr 15 '14 at 19:01
  • Windows 7, right-click on folder with a bunch of files and it deletes everything after asking to put in to recycle bin. What am I missing? Does being in the Recycle bin not count?
    – JeffO
    Apr 16 '14 at 1:12

Many file systems work with a reference count system for files.

~/foo$ ls -l
total 312
-rwxr-xr-x  2 shagie  staff  14884 Jan 24 10:35 a.out*
-rwxr-xr-x  1 shagie  staff    379 Apr 10 13:56 alias.pl*
-rw-r--r--@ 1 shagie  staff  14236 Apr 10 13:50 aliases.csv
-rwxr-xr-x  2 shagie  staff  14884 Jan 24 10:35 b.out*
-rw-r--r--  1 shagie  staff    137 Feb 17 15:30 f.pl
-rwxr-xr-x  1 shagie  staff    616 Mar 24 15:19 file*

You've got permissions, and then the reference count. Note that a.out and b.out are the same file. Once all references to a file are deleted it can be reclaimed. But files and directories are different.

In POSIX, you've got files which are removed by unlink - which removes them from a directory entry.

The unlink function deletes the file name filename. If this is a file's sole name, the file itself is also deleted. (Actually, if any process has the file open when this happens, deletion is postponed until all processes have closed the file.)

And you've got directories which are removed with rmdir.

The rmdir function deletes a directory. The directory must be empty before it can be removed; in other words, it can only contain entries for . and ...

These are separate checks and rmdir can't remove a file nor can unlink remove a directory. That's just the way it works.

This makes the code much simpler and fewer error messages involved. A function does one and only one thing. From Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?

If, when describing the activity of the code to another programmer you use the word 'and', the method needs to be split into at least one more part.

For rmdir to behave this way the description would be:

Remove the directory entry, and any files if any are contained within the directory.

Note the and. By making rmdir non-atomic this means there are many more situations where a partial process could be done without clearly saying if it was done or not. If you hit files you cannot delete as part of the hypothetical rmalldir is it a success? failure? what error message?

And thus, each function call does one and only one thing - remove a file. remove a directory. Any errors are specifically applicable to that operation.

The code is simpler and there are fewer possible situations for error either in the library or people using the library.

  • I like the reference to the Unix philosophy of "do one thing and do it well." Comparing rmdir to rm is a good idea because the underlying mechanics often get hidden underneath GUI abstractions.
    – user22815
    Apr 15 '14 at 18:37
  • @JohnGaughan I was actually working from the posix implementation there rather than the unix commands - man 2 rmdir rather than man 1 rmdir... just happens those two share the same name.
    – user40980
    Apr 15 '14 at 18:42
  • I doubt a Unix GUI file manager would issue the rmdir command via command prompt anyway.
    – user22815
    Apr 15 '14 at 18:47
  • @JohnGaughan hopefully not. But I was trying to get at that the restrictions are done at a lower level (section 2 from the man pages) and ultimately make for simpler code.
    – user40980
    Apr 15 '14 at 18:57
  • 1
    @ratchetfreak good point (although MichaelT was referring to Posix methods and not command line commands), although if you have ever seen an rm -r or deltree fail in the middle, it is a strong argument for not deleting recursively.
    – user22815
    Apr 15 '14 at 21:46

I think it's mostly for your protection, to avoid deleting a lot of files you don't mean to. You potentially could have lots of directories and files directories under the top=level one, and not know especially when working from the command line, which is where this convention started back before there were GUIs. Furthermore, I don't there is the concept of a Trask or Recycle bin available for files and folders deleted using the rm command.

As someone already pointed out, you can get around this behavior on Linux and Unix by providing a "-r" flag to the remove command, or even mask it by making rm a alias for "rm -r".

On GUI-based systems, it's much easier to see if you have folders or files underneath the one you are about to delete, plus you can recover files easier.

If you try to delete a folder on Windows, it asks first (like it does for any deletion), but then goes ahead and deletes the folder and all files and folders underneath without any further prompting. If you make a mistake, it's pretty trivial to restore your folders/files from the Recycle bin.

  • about that last point, not if there is no recycle bin like for flash disks Apr 15 '14 at 21:48
  • The last paragraph is rather false. Windows asks things like "blah.exe is an application, are you sure?", "blah is whatever, are you sure?" again and again and you need something like TeraCopy to get rid of it.
    – maaartinus
    Apr 15 '14 at 22:51
  • @maaartinus That's not my experience; I just made a copy of a folder containing dozens of commands (it's my c:\bin) and Windows only asked if I wanted to delete the folder, nothing else. I'm using Windows 7 on this particular computer, haven't tried it on Windows 8.
    – tcrosley
    Apr 16 '14 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.