Wondering how to use the diff command in Linux and want to know what the Linux diff command is? This article will help you understand the command well and answer the question about the usage of the diff command. The main usage of the diff command is to find the difference between two files in Linux.
Options for the diff command in Linux:
- -y used to display differences in files side by side
- -s This can be used in combination with other commands to notify if two files are the same
- -r If a directory is specified, this option will compare any subdirectories found.
- -i Since Linux is case sensitive, this option will ignore all case sensitive differences.
- -B Ignore blank lines and don’t show them as differences in the output
- -w Ignore whitespaces from the differences
Using Linux diff command to compare two files
Let’s create two files with similar content along with some minor differences. Here are the two files that I’m working with:
root@ubuntu:~# cat LinuxForDevices.txt This is some sample text I'm going to write a different line in another file root@ubuntu:~# cat LFD.txt This is some sample text I'm going to write a different line in another file named LFD.txt An additional line
As you can see, there are two files named LinuxForDevices.txt and LFD.txt where the only difference is the last line which adds the “named LFD.txt” part.
Let’s run the Linux diff command and find the differences between the two files.
root@ubuntu:~# diff LinuxForDevices.txt LFD.txt 3c3,4 < in another file --- > in another file named LFD.txt > An additional line
Without any arguments, it shows you the lines that are different in both the files. Let’s discuss the output a little more:
- The number 3c3, 4 stands for line 3 from file1 should change to line 3,4 from file2.
- The < marker signifies the first file (LinuxForDevices.txt) while > marker signifies file2 (LFD.txt)
- The hyphens in between are there to differentiate between the two files.
Show Differences Side-by-Side using diff Command in Linux
In the above example, you saw that the command output displays the results one after the other. But for larger files, it would be better to see the results side by side. This can be done with the -y option.
root@ubuntu:~# diff -y LinuxForDevices.txt LFD.txt
Adding the -y option to the Linux diff command will display the output as below now.
root@ubuntu:~# diff -y LinuxForDevices.txt LFD.txt This is some sample text This is some sample text I'm going to write a different line I'm going to write a different line in another file | in another file named LFD.txt > An additional line
Perfect, this is exactly what we wanted to see!
Notify About Identical Files with diff Command in Linux
By default, if the files are exactly the same, the Linux diff command displays no output. But adding the -s option will print an output stating that the two files are identical.
For demonstration, I created a copy of the file LFD.txt and named it as LFD-copy.txt
root@ubuntu:~# diff -s LFD.txt LFD-copy.txt
As you can see in the screenshot above, when I ran the command without the -s option, the command produced no output.
Directory Content Differences Using the Linux diff Command
Instead of finding the differences in files, if you’re working with multiple directories with a lot of files, the diff command can help you identify the files that are present in one directory and not in the other. This can be achieved using the -r command option.
As we discussed above, the case sensitive nature of Linux will also consider the same filenames with a different case as two different files. To avoid this, we can use the –ignore-file-name-case option.
To demonstrate this, I’ve created a folder with two sub-folders using the mkdir command and moved our 3 files in those. Here’s what my directory and file structure is.
Now the next step is to find identify the differences between the two directories using the diff command which we’ll do using the -r command option.
root@ubuntu:~# diff -r folder-1/ folder-1/folder-2/ Only in folder-1/: folder-2 Only in folder-1/: folder-3 Only in folder-1/: LFD.txt Only in folder-1/folder-2/: LinuxForDevices.txt
This directory structure is very simple but will prove the point of how the recursion is working. This command option allows the diff command to look into the directory and compare all the files and directories within it.
Ignore Blank Links and White Spaces with diff Command in Linux
Some files will simply have a lot of blank lines and whitespaces which will unnecessarily load up the screen by highlighting blanks as differences. We can ignore these blanks to focus on what matters using the -B and the -w options. Let’s see how the outputs differ when we work with the two commands.
I’ve added extra blank lines in our LinuxForDevices.txt file.
root@ubuntu:~# cat LinuxForDevices.txt This is some sample text I'm going to write a different line in another file
Now let’s see what the diff command in Linux shows as output.
As you see, the blank lines are highlighted as differences and the first line says 2d1 which means 2nd line needs to be deleted from the first file.
root@ubuntu:~# diff -B LinuxForDevices.txt LFD.txt 4,5c3,4 < < in another file --- > in another file named LFD.txt > An additional line
As you can see, the first line which informed us to delete the 2nd line no longer shows. But one blank line from LinuxForDevices.txt still shows up. That’s because we are missing an extra line of text from the LFD.txt file. It wants us to replace the last two lines from LinuxForDevices with the last two lines of LFD.txt to make them the same.
If we replace the -B with -w, instead of ignoring blank lines, it will ignore whitespaces not entire blank lines. But if you want to the output to ignore both blank lines and whitespaces, you can use the command diff -Bw.
Ignore Text Case Using the diff Command in Linux
To demonstrate and explain case sensitivity, we’ll create two new files with just a single word, “Hello”. I’ve created two files 1.txt and 2.txt with the words “Hello” and “hello” respectively.
If I run the diff command without any arguments, you’ll see that it highlights the case difference between the words as the “difference” between the files.
root@ubuntu:~# diff 1.txt 2.txt 1c1 < Hello --- > hello
But with the simple addition of -i will ignore this difference and the files will be considered identical. As we’ve seen earlier that there’s no output for identical files, we’ll also add the -s option.
root@ubuntu:~# diff -s -i 1.txt 2.txt Files 1.txt and 2.txt are identical
Perfect! This is exactly what we expected.
To end this tutorial on the diff command in Linux, I’d like to let you know that the different options that I demonstrated above are not to be considered as independent of each other. Though they don’t really need to work alongside the other options, you should definitely combine the options that suit your requirements.
And though this list of demonstrations should fulfill most of your needs, you should certainly check out the man page for this command using the man command.