Recursive deletion: Which rm command removes all files in the current directory as well as every file and subdirectory within its subdirectories?

Difficulty: Medium

Correct Answer: rm -r *

Explanation:


Introduction / Context:
Recursive deletion is a powerful but dangerous operation on UNIX systems. The rm utility can remove directories and their contents when used with the appropriate options. Proper understanding prevents accidental data loss and ensures safe automation in scripts.



Given Data / Assumptions:

  • We want to delete files in the current directory and everything nested beneath it (subdirectories and files).
  • We are not specifically handling hidden files; patterns can be adjusted.
  • We have the necessary permissions and understand the risk.


Concept / Approach:

rm -r enables recursive removal of directories. When combined with a wildcard like , it targets all non-hidden entries in the current directory, descending into directories to remove their contents as well. Add -i to prompt before each removal or -f to force without prompts based on safety needs.



Step-by-Step Solution:

Confirm you are in the correct directory (pwd).List contents (ls -A) before proceeding.Run rm -r * to remove all non-hidden entries recursively.Consider rm -r . carefully if hidden entries must be removed; beware of . and .. exclusions.Verify with ls afterward to ensure intended removal.


Verification / Alternative check:

Test on a disposable directory structure created with mkdir -p and touch to confirm behavior. Use tree or find to verify no remnants remain.



Why Other Options Are Wrong:

a: rm * removes only files (and empty directories in some shells with special options) in the current directory, not recursive subdirectories.

c: rm all is not a standard command.

d: rm . is DOS-style and misses names without dots; still not recursive.

e: Not applicable because rm -r * is correct.



Common Pitfalls:

Running from the wrong directory; forgetting hidden files; omitting quotes when using dangerous patterns in scripts; not using -i for interactive confirmation in sensitive locations.



Final Answer:

rm -r *

Discussion & Comments

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