We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
Linux Practices: Using diff and cmp
Using diff
Practice 1: Comparing Two Files for Differences
Objective: Use diff to identify differences between two text files. Instructions: 1. Create two files named Avengers1.txt and Avengers2.txt: Avengers1.txt: IronMan Thor Hulk Avengers2.txt: IronMan Thor BlackWidow 2. Compare the files: diff Avengers1.txt Avengers2.txt 3. Verify that diff shows the differing lines.
Practice 2: Ignoring Case Sensitivity
Objective: Use diff to compare files while ignoring case differences. Instructions: 1. Create the following files: File1.txt: IronMan THOR Hulk File2.txt: ironman Thor Hulk 2. Compare the files ignoring case differences: diff -i File1.txt File2.txt 3. Verify that diff reports no differences.
Practice 3: Ignoring White Space
Objective: Use diff to compare files while ignoring white spaces. Instructions: 1. Create two files where one has extra spaces: File1.txt: Thor Hulk File2.txt: Thor Hulk 2. Compare the files ignoring white spaces: diff -w File1.txt File2.txt 3. Verify that diff does not detect any differences.
Practice 4: Using Side-by-Side Output
Objective: Use diff to display differences side by side. Instructions: 1. Compare Avengers1.txt and Avengers2.txt using side-by-side output: diff -y Avengers1.txt Avengers2.txt 2. Verify that the output shows both files side by side with differences clearly marked.
Practice 5: Displaying Differences in Context
Objective: Use diff to show contextual differences between files. Instructions: 1. Compare Avengers1.txt and Avengers2.txt with context: diff -c Avengers1.txt Avengers2.txt 2. Verify that the output shows lines before and after the differences for context.
Using cmp
Practice 6: Comparing Binary Files
Objective: Use cmp to compare two binary files. Instructions: 1. Create two binary files: echo -n "IronMan" > Binary1.bin echo -n "IronMan" > Binary2.bin 2. Compare the files: cmp Binary1.bin Binary2.bin 3. Verify that cmp reports no differences.
Practice 7: Detecting the First Difference
Objective: Use cmp to find the first byte difference between files. Instructions: 1. Modify Binary2.bin: echo -n "Thor" > Binary2.bin 2. Compare the files: cmp Binary1.bin Binary2.bin 3. Verify that cmp reports the first differing byte and its position.
Practice 8: Comparing Text Files with cmp
Objective: Use cmp to compare text files line by line. Instructions: 1. Create the files Heroes1.txt and Heroes2.txt: Heroes1.txt: Thor Hulk Heroes2.txt: Thor BlackWidow 2. Compare the files: cmp Heroes1.txt Heroes2.txt 3. Verify that cmp identifies the first differing line.
Practice 9: Suppressing Output
Objective: Use cmp to suppress output and only return a result. Instructions: 1. Compare Heroes1.txt and Heroes2.txt with suppressed output: cmp -s Heroes1.txt Heroes2.txt 2. Verify that cmp returns no output if files are identical, or a status if they differ.
Practice 10: Combining diff and cmp
Objective: Use both diff and cmp to compare files and understand their outputs. Instructions: 1. Compare Avengers1.txt and Avengers2.txt using diff: diff Avengers1.txt Avengers2.txt 2. Then, compare the same files using cmp: cmp Avengers1.txt Avengers2.txt 3. Verify that diff shows detailed differences while cmp reports the position of the first difference.