The diff
command is used to compare and display the differences between two files or directories. Here’s how you can use it:
To compare two files, open your terminal and type:
diff {{file1}} {{file2}}
Replace {{file1}}
and {{file2}}
with the names of the files you want to compare. For example, diff file1.txt file2.txt
.
The diff
command will display the differences between the two files, indicating which lines have been added, modified, or deleted. The output will show the specific changes made to each line.
If you want to ignore whitespace changes, you can use the -w
option:
diff -w {{file1}} {{file2}}
To compare two directories, use the -r
option to perform a recursive comparison:
diff -r {{dir1}} {{dir2}}
Replace {{dir1}}
and {{dir2}}
with the names of the directories you want to compare. The diff
command will recursively compare the files in the directories and display the differences.
By default, diff
displays the differences in a side-by-side format. If you prefer a unified diff format, you can use the -u
option:
diff -u {{file1}} {{file2}}
You can also save the output of diff
to a file using the redirection operator >
:
diff {{file1}} {{file2}} > diff_output.txt
This will save the differences between file1
and file2
to a file named diff_output.txt
.
These are just a few examples of how to use the diff
command. You can refer to the manual page by typing man diff
in your terminal for more information and additional options.
Remember that diff
is a powerful tool for comparing files and directories, and it can be very useful for tracking changes or identifying differences between versions of files.