cat (display file contents) in Linux CLI - Time & Space Complexity
When using the cat command to display file contents, it is helpful to understand how the time it takes grows as the file size grows.
We want to know how the command's work changes when the file gets bigger.
Analyze the time complexity of the following code snippet.
cat filename.txt
This command reads and shows the entire content of filename.txt on the screen.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each byte or line from the file one by one.
- How many times: Once for every byte or line in the file, depending on implementation.
As the file size grows, the number of bytes to read grows too, so the work grows in a straight line with file size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 bytes | About 10 read steps |
| 100 bytes | About 100 read steps |
| 1000 bytes | About 1000 read steps |
Pattern observation: The work grows directly in proportion to the file size.
Time Complexity: O(n)
This means the time to display the file grows linearly as the file gets bigger.
[X] Wrong: "The cat command runs instantly no matter the file size."
[OK] Correct: Actually, cat must read every part of the file, so bigger files take more time.
Understanding how commands like cat scale with input size helps you think clearly about performance in real tasks.
"What if we used head -n 10 filename.txt instead? How would the time complexity change?"
Practice
cat command do in Linux?Solution
Step 1: Understand the purpose of
Thecatcatcommand 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 likerm,chmod, andmkdir.Final Answer:
Displays the contents of a file on the terminal -> Option BQuick Check:
catshows file contents [OK]
- Confusing cat with rm or mkdir
- Thinking cat edits files
- Assuming cat deletes files
notes.txt using cat?Solution
Step 1: Identify the basic syntax of
The basic command to display a file iscatcat filename, socat notes.txtis correct.Step 2: Understand incorrect options
-fand-rare not valid options forcat. Thecat > filenamesyntax is used to create or overwrite a file, not display it.Final Answer:
cat notes.txt -> Option AQuick Check:
Basic syntax iscat filename[OK]
- Adding invalid options like -f or -r
- Using > which overwrites files
- Confusing cat with other commands
cat -n file.txt if file.txt contains:Hello World
Solution
Step 1: Understand the
The-noption-noption 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 DQuick Check:
cat -nadds line numbers [OK]
- Ignoring line numbers
- Placing numbers after text
- Thinking -n is invalid
cat file1.txt file2.txt but get an error: cat: file2.txt: No such file or directory. What is the most likely cause?Solution
Step 1: Analyze the error message
The error saysfile2.txtis missing, so the system cannot find it in the current directory.Step 2: Check other options
Emptyfile1.txtor permission issues onfile1.txtwould not cause this error. The syntax is correct for displaying multiple files.Final Answer:
file2.txt does not exist in the current directory -> Option CQuick Check:
Missing file causes 'No such file' error [OK]
- Assuming syntax error
- Ignoring missing file
- Blaming permissions wrongly
chapter1.txt and chapter2.txt into a new file book.txt. Which command correctly does this using cat?Solution
Step 1: Understand output redirection with
The commandcatcat file1 file2 > newfileconcatenates file1 and file2 contents and writes to newfile.Step 2: Analyze each option
cat chapter1.txt chapter2.txt > book.txt correctly concatenateschapter1.txtandchapter2.txtintobook.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 AQuick Check:
Usecat file1 file2 > newfileto combine files [OK]
- Misplacing > causing overwrite
- Using > multiple times incorrectly
- Confusing input and output files
Ready to prove it?
Pass the test and this pattern moves unknown
