Diff a Directory for Only Files of a Specific Type on Linux


In this article, we are going to learn how to Diff a directory for only files of a specific type in Linux.

Abstract

In Linux, a frequently used operation is used for comparing files and identifying their differences. It is very helpful while comparing complicated code or in the configuration of files. Linux has a powerful built-in tool called diff that is used to compare directories It is error-free and also saves time.

Here are a few examples that will help us learn how to apply these intriguing and adaptable commands,

Diff command

Using the Diff command, we can compare the contents of two files line by line, and if there are any differences, it will list them along with the corresponding line numbers. In addition, it can be used to contrast the contents of two directories.

In shell scripts, the diff command is crucial when we wish to execute an action based on the comparison of two files. In order to quickly compare the two directories, we'll use the diff command as in the following example −

Example

$ diff dev home

Output

Only in home: cg
Only in dev: core
Only in dev: fd
Only in dev: full
Only in dev: mqueue
Only in dev: null
Only in home: objc
Only in dev: ptmx
Only in dev: pts
Only in dev: random
Only in home: redxx
Only in dev: shm
Only in dev: stderr
Only in dev: stdin
Only in dev: stdout
Only in dev: tty
Only in dev: urandom
Only in dev: zero

As we can see from the output, the contents of the two directories are listed above and show how they differ.

In another example, we will see how to use different variations with the diff command, Here we will use “-y” to get the information in the side views, this will give a better view for comparison and understanding the difference,

Example

$ diff -y tclConfig.sh tclooConfig.sh

Output

TCL_MAJOR_VERSION='8' | TCLOO_LIB_SPEC="7"
TCL_MINOR_VERSION='6' | TCLOO_STUB_LIB_SPEC="4"
TCL_PATCH_LEVEL='.8'  | TCLOO_INCLUDE_SPEC="6"
TCL_VERSION='8.6'     | TCLOO_PRIVATE_INCLUDE="1"

Here is another example showing how to use different variation with the diff command. Here we will use “-u” to display output without any extra context lines, this will give specific information about the difference,

Example

$ diff -u file1.sh file2.sh

Output

––– file1.sh 2019-05-11 15:50:49.000000000 +0000
+++ file2.sh 2019-05-11 15:50:43.000000000 +0000
@@ -1 3, +1 5
It will create shell variables
This script is intended to be included
The information in this file is specific
+These are mostly empty
+All libraries and include files are just part of Tcl

In the output, we can clearly notice the two files' differences in the output from the previous step. file2 has an extra two lines at the conclusion, as we all know. These lines are highlighted in the output by having a plus sign (+) before them. Since file1 came before file2 in the command above, the diff command compared the files from file1's perspective.

Conclusion

In this article we learned about the use cases of the Diff command. First, we saw the actual use of the Diff command i.e., how to use it to differentiate between two directories.

These are the scenarios where the Diff command is used in Linux. I hope you find these examples of the commands useful and that it's easy to learn and use Linux.

Updated on: 19-Dec-2022

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements