Home > Article > Operation and Maintenance > Different ways to compare files in linux
#The Linux file comparison command is used to compare the differences in the contents of two files. There are mainly comm command and diff command. If you want to compare two ordered files, you can use the comm command. If you want to compare two text files line by line and list their differences, you can use the diff command.
Use the common command to compare:
The comm command is a very concise command with only two parameters. However, the three parameters are often used in combination. Our common usage is as follows:
comm - 12 only displays lines that exist in both files;
comm - 23 only displays Lines that appear in the first file but not in the second file;
comm Find the lines that are in file 2 but not in file 1:
comm -23 2 .txt 1.txt
Note: The contents of the two files must be sorted before comparing them. Otherwise the output results will be wrong.
diff command comparison:
The diff command is a classic text comparison tool. The diff command has more parameters than comm. It is often used in combination with the patch command to perform patch upgrades. By default, the -a parameter is used, which compares the differences between the two files line by line. To achieve the desired results here, we also need to use grep and awk:
diff 2.txt 1.txt |grep "<"|awk ' $1 = " " '
Note: It is also found here that when using the diff command to compare, the compared files also need to be sorted in advance, otherwise the output results will also be incorrect.
Recommended learning video tutorial: linux video tutorial
The above is the detailed content of Different ways to compare files in linux. For more information, please follow other related articles on the PHP Chinese website!