Copying files in UNIX/Linux Which command copies a regular file (and, with flags, can also copy directories)?

Difficulty: Easy

Correct Answer: cp

Explanation:


Introduction / Context:
File copying is foundational in system administration and scripting. UNIX/Linux uses a concise, portable command to copy files, preserve attributes, and recursively copy directory trees with optional flags for safety and verbosity.


Given Data / Assumptions:

  • You need to copy files between paths on the same system.
  • Basic permissions allow reading source and writing destination.
  • Standard POSIX toolset available.


Concept / Approach:
The cp command copies files. Common flags include -r or -R for recursive directory copy, -p to preserve mode, ownership, and timestamps, -i to prompt before overwrite, and -v for verbose progress. For archival purposes, tools like tar or cpio are preferable, but for direct filesystem copies, cp is primary.


Step-by-Step Solution:
Copy a single file: cp source.txt dest.txtCopy into a directory: cp source.txt /path/dir/Recursive directory copy: cp -R srcdir dstdirPreserve attributes: cp -p source dest


Verification / Alternative check:
Use ls -l and diff or checksums (md5sum/sha256sum) to verify results. For maintaining sparse files or extended attributes, consult distro-specific flags or use rsync.


Why Other Options Are Wrong:
copy (Option A) is a DOS/Windows command, not UNIX.cpio (Option C) and tar (Option D) are archiving tools rather than simple copy utilities.


Common Pitfalls:

  • Overwriting files unintentionally; use -i or backups.
  • Forgetting -R when copying directories, resulting in “omitting directory”.
  • Losing timestamps or ownership without -p when that metadata matters.


Final Answer:
cp

Discussion & Comments

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