This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Comparing Files: comm, cmp and diff Command”.
1. Which command is used for comparing two files?
Explanation: cmp command is used for comparing two files byte by byte. It displays the first mismatch and its location on the terminal. It does not bother about subsequent mismatches but displays only the first mismatch. As soon as the first difference is found, the command terminates and outputs the line and byte in which there is a difference. For example,
$ cmp file01 file02 file01 file02 differ: Byte 20, Line 2
diff command is used for converting one file into another in order to make them identical and comm is used for displaying the common elements in both the files.
2. What is the exit status of cmp command if both the files are identical?
Explanation: The exit status of cmp command is 0 if both the files are identical and 1 if both the files are different. The exit status is 2 if the program encounters a problem.
3. Which option is used with cmp command to display a detailed list of mismatches?
Explanation: cmp command by default only displays the first mismatch that occurs in both files. For a detailed list of all the mismatches, we use the -l option. It prints byte position and the byte value for all differing bytes.
4. Which option is used with cmp command to limit the number of bytes to be compared?
Explanation: We can limit the number of bytes to be compared by the cmp command by using -n option with it. For example, if we want to compare at most 50 bytes then the command will be written as:
$ cmp -n 50 file01 file02
5. How can we skip some initial bytes from both the files to be compared?
a) using -b option
b) using -n option
c) using -l option
d) using -i option
Explanation: We can skip the number of initial bytes to be compared from both the files such that the cmp command will not consider that number of bytes while comparing both files. For this facility, we use -i option followed by the number of bytes to be skipped. For example, to skip the first 10 bytes use the following command,
cmp -i 10 file01 file02
6. -b option is used with cmp command to print the differing bytes in both files.
Explanation: -b option when used with cmp command print the byte number and the line number along with the differing bytes, where the first mismatch occur. For example,
$ cmp -b file01 file02 file01 file02 differ: Byte 20, Line 2 is 256 z 268 w
In the above output, the difference is in 17th byte, which is ‘z’ in file01 and ‘w’ in file02 respectively. The values 256 and 268 are the values for these bytes.
7. Which command compares two sorted files line by line?
Explanation: comm command is used for comparing two sorted files line by line. When there is no option specified with comm command it produces three column output. For example, if we want to compare two files namely file01 and file02 then the first column will contain those entries which are unique to file01 whereas the second column will contain those entries which are unique to file02. The third column will contain the actual information i.e. those entries which are common to both fil01 and file02.
$ comm file01 file02 Sanfoundry Project Global Learning UNIX
In the above output, ‘Sanfoundry’ and ‘Learning’ are unique in file01 while ‘Global’ and ‘UNIX’ are unique in file02 whereas ‘Project’ is common to both the file.
8. What will be the command if we want to suppress column 1 and column 2 in the output of comm Command?
a) comm -12
b) comm -1-2
c) comm -3
d) comm -1,-2
Explanation: comm command provides us with an option for suppressing the columns in the output. For this task, we have to provide the column number as an option in the comm command. For example, -1 for column 1, -2 for column 2 and -3 for column 3.
9. For comm command to work properly, files specified should be sorted.
Explanation: comm command only compares those files that are already sorted. If the files are not sorted it will throw an error message displaying “file not in sorted order”.
10. Which command is used to display the differences between files?
Explanation: diff command is used for comparing files and displaying the differences between them.
diff command uses certain symbols and instructions to indicate the changes that are required to make two files identical. For example,
a – for appending
c – for changing
d – for deleting
11. When two files are identical, what is the output of diff command?
a) Files are identical
b) Returns prompt
c) No output
d) Throws an error
Explanation: When two files are identical, diff command does not produce any output. It simply returns the shell prompt $. However, we can use the -s option to display an informative message on the terminal if the files are identical.
12. Which option is used to produce multi columnar output in diff command?
Explanation: -y option when used with diff command produces output in multiple columns. For example,
$ diff -y file01 file02 Hii Hii Hello | Hell world world Here ( | ) symbol indicates that the lines are different in both files.
13. If we are interested in knowing that whether the two files are identical or not, we can use ___ command.
Explanation: All the three commands comm, diff and cmp are used for comparing files but if we are interested in knowing whether two files are identical or not we can use comm command without any option. It will simply return the prompt without displaying any output if both the files are identical.
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.