RMDIR(2)

πŸ’– RMDIR(2) πŸ’–

okay bestie, so this one is a page from the System Calls Manual (yes, the 2 section, we love a syscall moment) and it's all about the rmdir system call. let's get into it 🫢


✨ NAME ✨

rmdir β€” remove a directory file. that's it. that's the whole vibe. bye-bye empty folder πŸ’…


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc). so like, link against -lc and you're golden ✨


πŸŽ€ SYNOPSIS πŸŽ€

#include <unistd.h>

int
rmdir(const char *path);

like, that's the whole signature. one path, one int coming back at you πŸ’œ


πŸ¦‹ DESCRIPTION πŸ¦‹

so the rmdir() system call removes a directory file whose name is given by path.

and here's the tea β˜• β€” the directory must not have any entries other than β€˜.’ and β€˜..’. it has to be empty empty. not "empty-ish". not "just one little file". literally just . and .. and nothing else. if there's anything else in there, bestie, it's not happening 😭


🌈 RETURN VALUES 🌈

  • rmdir() returns the value 0 if it succeeded. slay ✨
  • otherwise the value -1 comes back and the global variable errno is set to tell you exactly what went wrong. iconic error-reporting behaviour, honestly πŸ‘‘

πŸ‘‘ ERRORS πŸ‘‘

so the named file is removed unless one of these lil guys happens. read 'em and weep:

  • [ENOTDIR] β€” a component of the path is not a directory. rude of it.
  • [ENAMETOOLONG] β€” a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. that's a LOT of characters bestie, calm down 😭
  • [ENOENT] β€” the named directory does not exist. it's giving "who?" πŸ’€
  • [ELOOP] β€” too many symbolic links were encountered in translating the pathname. the symlinks got into a situationship and never came out πŸŒ€
  • [ENOTEMPTY] β€” the named directory contains files other than β€˜.’ and β€˜..’ in it. we told you it had to be empty 😀
  • [EACCES] β€” search permission is denied for a component of the path prefix.
  • [EACCES] β€” write permission is denied on the directory containing the link to be removed.
  • [EPERM] β€” the directory to be removed has its immutable, undeletable or append-only flag set, see the chflags(2) manual page for more information. yeah it's flagged, you cannot simply delete it 🚩
  • [EPERM] β€” the parent directory of the directory to be removed has its immutable or append-only flag set. the parent said "not on my watch" πŸ™ˆ
  • [EPERM] β€” the directory containing the directory to be removed is marked sticky, and neither the containing directory nor the directory to be removed are owned by the effective user ID. sticky-bit drama, we love to see it πŸ“
  • [EINVAL] β€” the last component of the path is β€˜.’ or β€˜..’. nope. absolutely not. you cannot rmdir the dot-dots, that's the rules 🚫
  • [EBUSY] β€” the directory to be removed is the mount point for a mounted file system. busy busy busy, it's got a whole filesystem hanging off it πŸ’…
  • [EIO] β€” an I/O error occurred while deleting the directory entry or deallocating the inode. hardware said nope 😭
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system. πŸ’€πŸ’€πŸ’€
  • [EROFS] β€” the directory entry to be removed resides on a read-only file system. it's read-only babes, you literally cannot βœ‹
  • [EFAULT] β€” the path argument points outside the process's allocated address space. the pointer went somewhere it should not have gone, main character energy gone wrong 🫠

🌸 SEE ALSO 🌸

mkdir(2), unlink(2) β€” the make-the-folder one and the delete-the-file one. the whole bestie trio fr fr 🫢


⭐ HISTORY ⭐

the rmdir() system call appeared in 4.2BSD. vintage! retro! still here! πŸ‘‘


ngl bestie, if you remember ONE thing: the directory must be empty except for β€˜.’ and β€˜..’. that's the whole personality of this syscall. no cap. πŸ’–

and that's the page!! this one came from Debian, dated March 30, 2020, and it lives at RMDIR(2). mwah, go delete an empty folder responsibly πŸŽ€βœ¨πŸ’œ

πŸ“¦ where this page comes from