UNIX basics: Which command removes (deletes) an empty directory from the filesystem?

Difficulty: Easy

Correct Answer: rmdir

Explanation:

Introduction / Context:Managing directories is a routine UNIX/Linux task. When a directory is no longer needed and is empty, you can remove it with a specific command. Using the proper utility prevents accidental data loss and ensures compatibility across systems.

Given Data / Assumptions:

  • The directory to be removed is empty.
  • You have the necessary permissions on the parent directory.
  • You are using standard POSIX tools.

Concept / Approach:The rmdir command removes empty directories. If the directory contains files or subdirectories, rmdir will fail. For non-empty directories, use rm -r carefully. Some systems offer convenience flags like rmdir -p to remove parent directories if they become empty, but the fundamental rule remains: rmdir only works on empty directories.

Step-by-Step Solution:

Ensure the directory is empty: ls DIR should show no entries (other than . and ..).Run: rmdir DIRIf removing a nested empty path, consider rmdir -p parent/child.Verify by listing the parent directory.

Verification / Alternative check:If rmdir fails with “Directory not empty,” remove contents first or use rm -r with extreme caution. Confirm permissions with ls -ld on the parent directory since write/execute on the parent affect deletion.

Why Other Options Are Wrong:rd, remove, rdir: not standard POSIX commands; may exist on other platforms or shells as aliases, but not portable UNIX. None of the above: incorrect because rmdir is correct.

Common Pitfalls:Attempting to remove non-empty directories with rmdir; lacking write permission on the parent directory; accidentally using rm -r without double-checking contents.

Final Answer:rmdir

More Questions from Unix

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion