Bird
Raised Fist0
Linux CLIscripting~20 mins

diff for file comparison in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Diff Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of diff command with two similar files
Given two files file1.txt and file2.txt with the following contents:

file1.txt:
apple
banana
cherry

file2.txt:
apple
banana
date

What is the output of the command diff file1.txt file2.txt?
ANo output
B
2c2
< banana
---
> banana
C
3c3
< cherry
---
> date
D
1a2
> banana
Attempts:
2 left
💡 Hint
Look for the line number where the files differ and how diff shows changes.
💻 Command Output
intermediate
2:00remaining
Using diff with -u option
What is the main difference in output when running diff -u file1.txt file2.txt compared to diff file1.txt file2.txt for two files with small differences?
AOnly line numbers where files differ without content
BNo output if files differ
COutput in reverse order of lines
DUnified diff format showing context lines with '+' and '-' prefixes
Attempts:
2 left
💡 Hint
The -u option is often used for patches and shows more context.
💻 Command Output
advanced
2:00remaining
Output of diff with binary files
What output does the command diff file1.bin file2.bin produce if both files are binary and differ?
ALine by line text differences
BBinary files file1.bin and file2.bin differ
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
diff handles binary files differently than text files.
🔧 Debug
advanced
2:00remaining
Why does diff show no output for two different files?
You run diff fileA.txt fileB.txt but see no output, even though you know the files differ. Which of the following is the most likely reason?
AThe files differ only in whitespace and diff is run with default options
BThe files are identical
CThe diff command is broken
DThe files have different names
Attempts:
2 left
💡 Hint
No output from diff means the files are identical.
🚀 Application
expert
3:00remaining
Automate file difference report generation
You want to create a script that compares two directories dir1 and dir2 recursively and outputs a unified diff for each differing file into a single report file diff_report.txt. Which command pipeline best achieves this?
Adiff -ur dir1 dir2 > diff_report.txt
Bdiff -r dir1 dir2 | tee diff_report.txt
Cdiff -u dir1 dir2 > diff_report.txt
Ddiff dir1 dir2 > diff_report.txt
Attempts:
2 left
💡 Hint
You need recursive comparison with unified diff format.

Practice

(1/5)
1. What does the diff command do in Linux?
easy
A. It copies files from one directory to another.
B. It deletes duplicate files automatically.
C. It merges two files into one.
D. It compares two files line by line and shows differences.

Solution

  1. Step 1: Understand the purpose of diff

    The diff command is used to compare files line by line.
  2. Step 2: Identify what diff outputs

    It shows the differences between two files, not deleting, merging, or copying.
  3. Final Answer:

    It compares two files line by line and shows differences. -> Option D
  4. Quick Check:

    diff compares files [OK]
Hint: Remember: diff shows differences, it does not merge or delete [OK]
Common Mistakes:
  • Thinking diff merges files
  • Confusing diff with copy or delete commands
  • Assuming diff modifies files
2. Which of the following is the correct **basic** syntax to compare two files file1.txt and file2.txt using diff?
easy
A. diff file1.txt file2.txt
B. diff -c file1.txt file2.txt
C. diff file1.txt > file2.txt
D. diff -r file1.txt file2.txt

Solution

  1. Step 1: Identify basic diff syntax

    The basic syntax to compare two files is diff file1 file2.
  2. Step 2: Understand options

    diff -c file1.txt file2.txt uses -c (context), which is valid but not basic syntax. diff file1.txt > file2.txt redirects output incorrectly. diff -r file1.txt file2.txt (-r) is for directories.
  3. Final Answer:

    diff file1.txt file2.txt -> Option A
  4. Quick Check:

    Basic diff syntax [OK]
Hint: Basic diff syntax: diff file1 file2 [OK]
Common Mistakes:
  • Using redirection instead of arguments
  • Confusing directory options with file comparison
  • Adding unnecessary flags for simple comparison
3. Given two files with these contents:

file1.txt:
apple
banana
cherry

file2.txt:
apple
banana
date

What will be the output of diff file1.txt file2.txt?
medium
A. 2c2 < banana --- > banana
B. No output (files are identical)
C. 3c3 < cherry --- > date
D. Error: files not found

Solution

  1. Step 1: Compare lines one by one

    Lines 1 and 2 are the same (apple, banana). Line 3 differs: 'cherry' vs 'date'.
  2. Step 2: Understand diff output format

    diff shows line number and change type: '3c3' means line 3 changed. Then shows old line with '<' and new line with '>'.
  3. Final Answer:

    3c3 < cherry --- > date -> Option C
  4. Quick Check:

    Line 3 differs [OK]
Hint: Look for first differing line number in diff output [OK]
Common Mistakes:
  • Assuming diff shows all lines
  • Confusing line numbers in output
  • Expecting no output for different files
4. You run diff file1.txt file2.txt but get the error: diff: file2.txt: No such file or directory. What is the most likely cause?
medium
A. file2.txt does not exist in the current directory.
B. You used wrong syntax for diff command.
C. file1.txt is empty.
D. You do not have permission to read file1.txt.

Solution

  1. Step 1: Analyze the error message

    The error says file2.txt: No such file or directory, meaning file2.txt is missing or path is wrong.
  2. Step 2: Check other options

    Wrong syntax would cause different errors. Empty file1.txt or permission issues on file1.txt won't cause missing file2.txt error.
  3. Final Answer:

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

    Missing file error [OK]
Hint: Check file existence if diff says 'No such file' [OK]
Common Mistakes:
  • Assuming syntax error causes missing file message
  • Ignoring file path or directory
  • Confusing permission errors with missing files
5. You want to compare two directories dir1 and dir2 recursively to find all file differences. Which diff command should you use?
hard
A. diff -c dir1 dir2
B. diff -r dir1 dir2
C. diff dir1 dir2
D. diff -i dir1 dir2

Solution

  1. Step 1: Understand recursive comparison

    The -r option tells diff to compare directories recursively.
  2. Step 2: Check other options

    -c is context output, not recursive. No option means file comparison only. -i ignores case, not recursive.
  3. Final Answer:

    diff -r dir1 dir2 -> Option B
  4. Quick Check:

    Recursive directory diff [OK]
Hint: Use -r to compare directories recursively with diff [OK]
Common Mistakes:
  • Using -c expecting recursion
  • Omitting -r for directory comparison
  • Confusing -i with recursive option
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