Copying a file between directories on UNIX: Which command correctly copies the file 'programs/wb' into the directory 'misc' keeping the same name?

Difficulty: Easy

Correct Answer: cp programs/wb misc

Explanation:


Introduction / Context:
On UNIX/Linux systems, file copy operations use the cp command. To copy a single file into a target directory preserving its filename, you specify the source path followed by the destination directory. Mastering cp syntax is essential for reliable file management and scripting.



Given Data / Assumptions:

  • Source: programs/wb.
  • Destination directory: misc.
  • We want to retain the filename wb.


Concept / Approach:

When the destination is a directory, cp places the source file inside it with the same name. If you wanted to rename the file, you would specify the new filename after the destination directory path. The command differs from DOS/Windows copy syntax and from archiving tools like tar.



Step-by-Step Solution:

Ensure the misc directory exists (mkdir -p misc if needed).Run cp programs/wb miscVerify success with ls -l misc/wbUse cp -i to prompt before overwrite, or cp -p to preserve timestamps/ownership if desired.For recursive directory copy, use cp -r (not required here).


Verification / Alternative check:

Compare checksums (for example, sha256sum programs/wb misc/wb) to confirm a perfect copy. If permissions differ, add cp -p.



Why Other Options Are Wrong:

a: copy is a DOS command, not UNIX cp.

c: tar is for archiving; not a simple copy.

d: DOS-style drive letters are not used on UNIX.

e: Not applicable, option b is correct.



Common Pitfalls:

Forgetting that an existing misc/wb may be overwritten; using relative paths from the wrong working directory; confusing cp with mv (move/rename).



Final Answer:

cp programs/wb misc

More Questions from Unix

Discussion & Comments

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