LFCS practice question 13 of 35

A user accidentally created the subdirectory \dir in his home directory. Which of the following commands will remove that directory?

Full set: 260 questions
Answer options
A: rmdir '~/\dir'
B: rmdir "~/\dir"
C: rmdir ~/'dir'
D: rmdir ~/\dir
E: rmdir ~/\\dir Correct answer
Correct Answer:
E. rmdir ~/\\dir
Explanation

The backslash (\) is a special character in the shell that acts as an escape character. To create or reference a file or directory name containing a literal backslash, that backslash must be escaped or quoted. In this case, the directory is named \dir. The command rmdir ~/\\dir correctly targets this directory. The ~ is expanded by the shell to the user's home directory path, and the \\ sequence is interpreted as a single, literal backslash, correctly forming the path to ~/\dir.

Why Incorrect

A. rmdir '~/\dir' fails because single quotes prevent the shell from expanding the tilde (~) to the home directory path.
B. rmdir "~/\dir" fails because inside double quotes, \d is interpreted simply as d, targeting the wrong directory (~/dir).
C. rmdir ~/'dir' targets a directory named dir, not \dir, as the backslash is not included within the quotes.
D. rmdir ~/\dir fails because the unquoted \d is interpreted by the shell as just d, targeting the wrong directory (~/dir).

References

1. GNU Bash Reference Manual, Section 3.1.2.1 "Escape Character": "A non-quoted backslash ‘\’ is the escape character. It preserves the literal value of the next character that follows...". This explains why \\ is needed to represent a literal \.

2. Sobell, M. G. (2017). A Practical Guide to Linux Commands, Editors, and Shell Programming (4th ed.). Pearson. Chapter 4, "The Bourne Again Shell," explains shell metacharacters and the use of the backslash for escaping.

3. MIT OpenCourseWare, 6.005 Software Construction, "Reading 1: The Shell". This reading covers shell basics, including special characters and the need for quoting or escaping them in filenames.

Scroll to Top