Bird
Raised Fist0
Linux CLIscripting~10 mins

cat (display file contents) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - cat (display file contents)
Start
↓
Run 'cat filename'
↓
Shell reads file
↓
File contents sent to stdout
↓
Display contents on screen
↓
End
The 'cat' command reads a file and shows its contents on the screen step-by-step.
Execution Sample
Linux CLI
cat example.txt
This command shows the contents of 'example.txt' on the screen.
Execution Table
StepActionInputOutputNotes
1Run commandcat example.txtNoneCommand starts
2Shell opens fileexample.txtFile data streamReads file contents
3Send data to stdoutFile data streamText linesData flows to screen
4Display outputText linesVisible file contentsUser sees file text
5EndNoneNoneCommand finishes
💡 File fully read and displayed, command ends
Variable Tracker
VariableStartAfter Step 2After Step 3Final
file_dataNoneFile contents loadedSent to stdoutDisplayed on screen
Key Moments - 2 Insights
Why does 'cat' show the whole file at once instead of line by line?
The execution_table row 3 shows 'cat' reads the entire file data stream before sending it to the screen, so it outputs all content continuously.
What happens if the file does not exist?
The shell cannot open the file (step 2 fails), and 'cat' shows an error message instead of file contents.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 3?
AText lines
BVisible file contents
CNone
DError message
💡 Hint
Check the 'Output' column at step 3 in the execution_table.
At which step does the file contents become visible on the screen?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Notes' column for when the user sees the file text.
If the file is empty, what would the output be at step 4?
AError message
BVisible file contents with text
CNo output (blank screen)
DPartial file contents
💡 Hint
Refer to variable_tracker for file_data state and execution_table step 4 output.
Concept Snapshot
cat filename

- Reads the whole file named 'filename'
- Sends contents to screen (stdout)
- Shows file text exactly as is
- If file missing, shows error
- Useful to quickly view file contents
Full Transcript
The 'cat' command in Linux reads a file and displays its contents on the screen. When you run 'cat filename', the shell opens the file, reads all its data, sends it to the screen, and then finishes. If the file does not exist, an error message appears instead. This command is simple and useful to quickly see what's inside a file.

Practice

(1/5)
1. What does the cat command do in Linux?
easy
A. Deletes a file permanently
B. Displays the contents of a file on the terminal
C. Changes file permissions
D. Creates a new directory

Solution

  1. Step 1: Understand the purpose of cat

    The cat command is used to read and display the contents of files in the terminal.
  2. Step 2: Compare with other options

    Deleting files, changing permissions, or creating directories are done by other commands like rm, chmod, and mkdir.
  3. Final Answer:

    Displays the contents of a file on the terminal -> Option B
  4. Quick Check:

    cat shows file contents [OK]
Hint: Remember: cat = concatenate and show file content [OK]
Common Mistakes:
  • Confusing cat with rm or mkdir
  • Thinking cat edits files
  • Assuming cat deletes files
2. Which of the following is the correct syntax to display the contents of a file named notes.txt using cat?
easy
A. cat notes.txt
B. cat -f notes.txt
C. cat > notes.txt
D. cat -r notes.txt

Solution

  1. Step 1: Identify the basic syntax of cat

    The basic command to display a file is cat filename, so cat notes.txt is correct.
  2. Step 2: Understand incorrect options

    -f and -r are not valid options for cat. The cat > filename syntax is used to create or overwrite a file, not display it.
  3. Final Answer:

    cat notes.txt -> Option A
  4. Quick Check:

    Basic syntax is cat filename [OK]
Hint: Use just 'cat filename' to show file content [OK]
Common Mistakes:
  • Adding invalid options like -f or -r
  • Using > which overwrites files
  • Confusing cat with other commands
3. What will be the output of the command cat -n file.txt if file.txt contains:
Hello
World
medium
A. Hello World
B. Hello 1 World 2
C. Error: invalid option
D. 1 Hello 2 World

Solution

  1. Step 1: Understand the -n option

    The -n option numbers each line of the output when displaying file contents.
  2. Step 2: Apply numbering to each line

    Given the file has two lines: "Hello" and "World", the output will prefix each line with its line number: "1 Hello" and "2 World".
  3. Final Answer:

    1 Hello 2 World -> Option D
  4. Quick Check:

    cat -n adds line numbers [OK]
Hint: Use -n to add line numbers to output [OK]
Common Mistakes:
  • Ignoring line numbers
  • Placing numbers after text
  • Thinking -n is invalid
4. You run cat file1.txt file2.txt but get an error: cat: file2.txt: No such file or directory. What is the most likely cause?
medium
A. You used the wrong command syntax
B. file1.txt is empty
C. file2.txt does not exist in the current directory
D. You do not have permission to read file1.txt

Solution

  1. Step 1: Analyze the error message

    The error says file2.txt is missing, so the system cannot find it in the current directory.
  2. Step 2: Check other options

    Empty file1.txt or permission issues on file1.txt would not cause this error. The syntax is correct for displaying multiple files.
  3. Final Answer:

    file2.txt does not exist in the current directory -> Option C
  4. Quick Check:

    Missing file causes 'No such file' error [OK]
Hint: Check file existence if 'No such file' error appears [OK]
Common Mistakes:
  • Assuming syntax error
  • Ignoring missing file
  • Blaming permissions wrongly
5. You want to combine the contents of chapter1.txt and chapter2.txt into a new file book.txt. Which command correctly does this using cat?
hard
A. cat chapter1.txt chapter2.txt > book.txt
B. cat > chapter1.txt chapter2.txt > book.txt
C. cat chapter1.txt > chapter2.txt > book.txt
D. cat book.txt > chapter1.txt chapter2.txt

Solution

  1. Step 1: Understand output redirection with cat

    The command cat file1 file2 > newfile concatenates file1 and file2 contents and writes to newfile.
  2. Step 2: Analyze each option

    cat chapter1.txt chapter2.txt > book.txt correctly concatenates chapter1.txt and chapter2.txt into book.txt. Options A, C, and D misuse redirection or file order, causing errors or overwrites.
  3. Final Answer:

    cat chapter1.txt chapter2.txt > book.txt -> Option A
  4. Quick Check:

    Use cat file1 file2 > newfile to combine files [OK]
Hint: Use > after files to save combined output [OK]
Common Mistakes:
  • Misplacing > causing overwrite
  • Using > multiple times incorrectly
  • Confusing input and output files
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard