In Unix/Linux filesystem management, which command removes a directory (typically only if it is empty)?

Difficulty: Easy

Correct Answer: rmdir

Explanation:


Introduction / Context:
Deleting directories is a routine task. Unix/Linux distinguishes between removing empty directories safely and recursively deleting directory trees. Knowing the correct command for empty directories prevents accidental data loss and aligns with least-destructive defaults.


Given Data / Assumptions:

  • You want to remove a directory that contains no files or subdirectories.
  • You prefer a command that fails safely if the directory is not empty.
  • No nonstandard aliases are assumed.


Concept / Approach:
Use 'rmdir dir_name' to remove an empty directory. If the directory is not empty, 'rmdir' will refuse and report an error, prompting you to verify contents with 'ls -A'. For removing non-empty directories, the more dangerous 'rm -r dir_name' is required, often paired with '-i' for safety or used cautiously.


Step-by-Step Solution:

Check contents: ls -A target_dirIf empty, remove: rmdir target_dirIf not empty, consider: rm -r target_dir (with care)Confirm with ls that the directory is gone.


Verification / Alternative check:
Attempting 'rmdir' on a directory with files should fail, protecting you from accidental recursive deletions. Use 'find target_dir -maxdepth 1 -type f' to verify emptiness before removal.


Why Other Options Are Wrong:

  • rd / dldir / rdir: Not standard Unix/Linux commands ('rd' is DOS/Windows).
  • None of the above: Incorrect because 'rmdir' is correct.


Common Pitfalls:
Confusing 'rmdir' with 'rm -r', forgetting hidden files may prevent 'rmdir', and lacking permissions to remove the directory.


Final Answer:
rmdir

Discussion & Comments

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