The cat command reads and displays the contents of a file on the screen. It can also combine multiple files into one output.
Click to reveal answer
beginner
How do you display the contents of a file named notes.txt using cat?
You type cat notes.txt in the terminal. This shows the full content of notes.txt on the screen.
Click to reveal answer
beginner
What happens if you use cat with multiple files like cat file1.txt file2.txt?
The contents of file1.txt and file2.txt are shown one after the other, combined in the output.
Click to reveal answer
intermediate
How can you create a new file using cat?
You can type cat > newfile.txt, then type your text. Press Ctrl+D to save and exit. This creates newfile.txt with your text.
Click to reveal answer
intermediate
What is a simple way to number the lines of a file when displaying it with cat?
Use cat -n filename. This shows the file content with line numbers before each line.
Click to reveal answer
What does the command cat file.txt do?
ARenames file.txt
BDeletes file.txt
CCopies file.txt
DDisplays the content of file.txt
✗ Incorrect
cat file.txt shows the content of the file on the screen.
How do you combine two files a.txt and b.txt into one output using cat?
Acat a.txt b.txt
Bcat a.txt + b.txt
Ccat -c a.txt b.txt
Dcat --merge a.txt b.txt
✗ Incorrect
cat a.txt b.txt outputs the contents of both files one after the other.
Which key combination do you press to finish typing text when creating a file with cat > file.txt?
ACtrl+D
BCtrl+Z
CCtrl+C
DCtrl+X
✗ Incorrect
Pressing Ctrl+D signals the end of input and saves the file.
What does cat -n file.txt do?
ACounts words in file.txt
BDeletes file.txt
CDisplays file.txt with line numbers
DCopies file.txt
✗ Incorrect
The -n option numbers each line of the file in the output.
If you want to quickly view a file's content, which command is best?
Atouch filename
Bcat filename
Cmkdir filename
Drm filename
✗ Incorrect
cat filename shows the file content. The others do different tasks.
Explain how to use cat to display a file and how to combine multiple files into one output.
Think about how you read one book page and then two books back to back.
You got /3 concepts.
Describe the steps to create a new text file using cat and how to save it.
Imagine writing a note and then closing the notebook to save it.
You got /3 concepts.
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
Step 1: Understand the purpose of cat
The cat command is used to read and display the contents of files in the terminal.
Step 2: Compare with other options
Deleting files, changing permissions, or creating directories are done by other commands like rm, chmod, and mkdir.
Final Answer:
Displays the contents of a file on the terminal -> Option B
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
Step 1: Identify the basic syntax of cat
The basic command to display a file is cat filename, so cat notes.txt is correct.
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.
Final Answer:
cat notes.txt -> Option A
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
Step 1: Understand the -n option
The -n option numbers each line of the output when displaying file contents.
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".
Final Answer:
1 Hello
2 World -> Option D
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
Step 1: Analyze the error message
The error says file2.txt is missing, so the system cannot find it in the current directory.
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.
Final Answer:
file2.txt does not exist in the current directory -> Option C
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
Step 1: Understand output redirection with cat
The command cat file1 file2 > newfile concatenates file1 and file2 contents and writes to newfile.
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.
Final Answer:
cat chapter1.txt chapter2.txt > book.txt -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.