We can use the
diff command to compare files line by line with a richer set of output formats than we did with
Simply pass in the two file names as the arguments to the
$ cat file1.txt
Life goals 1) Graduate High School 2) Go skydiving 3) Help and feed the poor and hungry 4) Run a half marathon 5) Swim at the beach 6) Travel to Ireland 7) Get rock solid abs
$ cat file2.txt
Life bucket list 1) Graduate college 2) Go skydiving 3) Help and feed the poor 4) Run a marathon 5) Swim at the beach 6) Travel the world
$ diff file1.txt file2.txt
1,2c1,2 < Life goals < 1) Graduate High School --- > Life bucket list > 1) Graduate college 4,5c4,5 < 3) Help and feed the poor and hungry < 4) Run a half marathon --- > 3) Help and feed the poor > 4) Run a marathon 7,8c7 < 6) Travel to Ireland < 7) Get rock solid abs --- > 6) Travel the world
The output will tell you how to change the first file to get the second file. Lines beginning with a < mean that they're from the first file, while the > mean they're from the second. The --- signifies separation of file1.txt and file2.txt.
There are three letters that signify three types of changes:
Thus in our example above, the
diff command tells us to change lines 1,2 from the file1.txt to lines 1,2 in the second file. The same for lines 4 and 5 (line 3 requires no change). Then it tells us to change lines 7,8 to just line 7 of file2.txt.
-u option, we can avoid redundant information. Again, the output will show you how to go from the first argument file to the second.
Here are a list of symbols used in unified mode.
$ diff -u file1.txt file2.txt
--- file1.txt 2015-06-03 22:34:30.000000000 -0700 +++ file2.txt 2015-06-03 22:38:26.000000000 -0700 @@ -1,8 +1,8 @@ -Life goals -1) Graduate High School +Life bucket list +1) Graduate college 2) Go skydiving -3) Help and feed the poor and hungry -4) Run a half marathon +3) Help and feed the poor +4) Run a marathon 5) Swim at the beach -6) Travel to Ireland -7) Get rock solid abs +6) Travel the world +
These a few common options used with the
diff command. Make sure you check the
man page for a complete listing.
Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.$ Check price
The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.$ Check price