On Unix/Linux, which command is commonly used both to display file contents and, via redirection, to create simple files?

Difficulty: Easy

Correct Answer: cat

Explanation:


Introduction / Context:
Unix utilities excel by doing one thing well and composing with redirection and pipelines. The classic tool for displaying file contents is also frequently used to create simple files by redirecting its output. Recognizing this pattern helps with quick notes, logs, and test data without invoking a full-screen editor.


Given Data / Assumptions:

  • We seek a command that can display file contents to standard output.
  • We also want to be able to create files using shell redirection.
  • Minimalism is preferred over full-screen editing.


Concept / Approach:

cat (concatenate) prints files to standard output. Combined with redirection, it can create files: for example, cat > notes.txt then typing lines and pressing CTRL+D saves the content. It can also join multiple files: cat a.txt b.txt > both.txt. Editors like vi and ed can create files too, but the question emphasizes a command used to display and—via shell features—create files quickly.


Step-by-Step Solution:

To display: cat file.txtTo create: cat > new.txt (type content, CTRL+D to end)To append: cat >> existing.txtTo concatenate: cat file1 file2 > merged.txt


Verification / Alternative check:

After creation, confirm with ls -l new.txt and cat new.txt to view the contents. This demonstrates both display and create behaviors in practice.


Why Other Options Are Wrong:

  • vi, ed: editors; they are used primarily for interactive editing, not simply displaying output to stdout by default.
  • lyrix: not a standard Unix command.
  • None of the above: incorrect because cat satisfies both uses.


Common Pitfalls:

  • Accidentally truncating files with >; use >> to append.
  • Using cat for large binaries to the terminal can corrupt display; redirect or use od/hexdump.


Final Answer:

cat.

Discussion & Comments

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