On Unix/Linux, which command is designed to copy directory structures in and out of archives or between devices while preserving metadata and hierarchy?

Difficulty: Easy

Correct Answer: cpio

Explanation:


Introduction / Context:
While cp copies files, tools like cpio and tar are designed for archiving and moving entire directory hierarchies with metadata preserved. Understanding when to use cpio helps with backups, migrations, and packaging tasks.


Given Data / Assumptions:

  • Goal: copy directory structures in and out (archiving/extraction or device-to-device).
  • We want to maintain hierarchy and attributes reliably.
  • Pipelines from find/xargs are acceptable.


Concept / Approach:

cpio reads file lists from standard input and writes archives to standard output or devices, and can also extract. Common patterns: find . -print | cpio -ov > backup.cpio and extraction with cpio -idv < backup.cpio. It is widely used for initramfs images and system backups.


Step-by-Step Solution:

Create archive: find /dir -depth -print | cpio -ov > dir.cpioExtract: cpio -idv < dir.cpioTo device: cpio -ov > /dev/st0 (for tape, historically)Preserve modes and ownership by running as root when necessary.


Verification / Alternative check:

List contents with cpio -itv < dir.cpio. Compare trees using diff -r or checksums after extraction.


Why Other Options Are Wrong:

  • copy: DOS/Windows command, not Unix.
  • cp and cp -p: copy files/directories but are not archival; -p preserves times/modes, yet cpio better suits archive workflows and device streams.
  • None: incorrect because cpio is correct.


Common Pitfalls:

  • Forgetting to use -depth with find to archive directories after their contents.
  • Incorrect relative paths causing extraction into unexpected locations.


Final Answer:

cpio.

Discussion & Comments

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