Which command copies all files whose names contain the string 'chap' followed by exactly two additional characters into the directory named 'progs'?

Difficulty: Easy

Correct Answer: cp chap?? progs

Explanation:


Introduction / Context:
Precise filename selection is a daily task in Unix shells. The question tests familiarity with glob wildcards—particularly the question mark, which matches exactly one character—so you can target filenames of a specific pattern without over-matching unintended files.


Given Data / Assumptions:

  • We need files matching: 'chap' plus exactly two characters (e.g., chap01, chapAB).
  • We must copy them into a directory named progs (relative or absolute path).
  • We are using the standard cp command.


Concept / Approach:

In shell globs, matches any number of characters (including zero), whereas ? matches exactly one character. Therefore, the pattern for “chap followed by exactly two characters” is chap??. To copy all such files into a destination directory, use cp chap?? progs. This ensures only two-character suffixes are matched and avoids catching longer names that would include.


Step-by-Step Solution:

Choose wildcard for exactly one character: ?Repeat twice to require two extra characters: ??Form the glob: chap??Copy to target: cp chap?? progs


Verification / Alternative check:

Dry-run with printf '%s\n' chap?? to preview matches. Confirm the destination exists (mkdir -p progs) before executing the copy.


Why Other Options Are Wrong:

  • cp chap progs: matches any length, not exactly two characters.
  • cp chap[12] /progs/.: mismatched intent; selects only 'chap1' or 'chap2' and wrong destination operand.
  • cp chap?? /progs/: invalid destination; you should specify the directory, not expand to its contents.
  • None of the above: incorrect because cp chap?? progs is correct.


Common Pitfalls:

  • Using and unintentionally copying too many files.
  • Forgetting to quote or preview when filenames may include spaces or special characters.


Final Answer:

cp chap?? progs.

Discussion & Comments

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