Archiving vs. copying directory trees on UNIX/Linux Which command is traditionally used to copy directory structures “in and out” of archives and is well suited for whole-tree backups and restores?

Difficulty: Easy

Correct Answer: cpio

Explanation:


Introduction / Context:
UNIX/Linux offers several tools for copying files and entire directory trees. While cp copies files and directories on the filesystem, cpio excels at moving directory structures into and out of archive streams, making it ideal for backups, restores, and piping with find and other tools.


Given Data / Assumptions:

  • You need to preserve directory structures and optionally metadata.
  • Streaming to devices or files (tapes, files, or pipes) is acceptable.
  • Familiarity with pipelines and redirection.


Concept / Approach:
cpio reads file lists from standard input and creates or extracts archives. Typical usage pairs find with cpio to archive an entire tree reliably. It supports various formats and can preserve ownership, modes, and timestamps when run with appropriate privileges and flags.


Step-by-Step Solution:
Create an archive: find /src -depth -print | cpio -ov > backup.cpioExtract an archive: cpio -idv < backup.cpioCopy across filesystems via pipe: (cd /src; find . -print) | (cd /dst; cpio -pdmv)Verify with ls -lR or checksums to ensure integrity post-restore.


Verification / Alternative check:
Compare with tar for similar functionality. While tar is more prevalent today, cpio remains standard on many UNIX systems and integrates cleanly with find-generated file lists.


Why Other Options Are Wrong:
cp (Option A) copies files/dirs directly but is not an archive in/out utility.cp -p (Option C) preserves mode/timestamps/ownership but still is cp, not an archiver.copy (Option D) is a DOS/Windows command, not UNIX.


Common Pitfalls:

  • Forgetting to supply file lists to cpio via stdin.
  • Not using -pdm when “pass-through” copying a tree between directories.
  • Neglecting ownership preservation without proper privileges.


Final Answer:
cpio

More Questions from Unix

Discussion & Comments

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