Diff between files in same directory structure in linux - linux

I have two directories that contains the same directory structure, also directory names are same (possibly different number of files) and how can I find out the differences between all the file contents and files in Linux?
Here is an example
\dir1
\subdir1
\file1
\subdir2
\file2
\file3
dir2
\subdir1
\file1
\subdir2
\file2
\file3
\file4
The content of file1 in dir1 and file1 in dir2 are different. The content of file2 in dir1 and file2 in dir2 are different. I can use
$diff dir1\subdir1\file1 dir2\subdir1\file1
$diff dir1\subdir1\file2 dir2\subdir1\file2
But I have to manually do the diff for each file. I would like to have an automatic way.

If you want to list the files being different, try:
diff -rq dir1 dir2
If you want to list the difference inside each file, remove the q:
diff -r dir1 dir2

Related

diff (GNU diffutils) 3.6 exclude directory

Is it possible to exclude directory from comparison by using diff?
there is an option -x to exclude that works ok with files:
diff -x "*.swp" dir1 dir2
but I could not get that -x working with paths of directories for eg:
diff -x "*/tmp/cache*" dir1 dir2
You need to specify only the last element :
diff -x "cache*" dir1 dir2

Update a file if it is common for two directory and delete it when its uniquely in one directory

Suppose Directory1 has
1.File1
2.File2
3.Subdirectory1
Subdirectory1 has :
3.1. File 3
3.2. File 4
and
Directory2 has
1.File1
2.File3
3.Subdirectory1
Subdirectory1 has :
3.1. File 3
3.2. File 6
If any file is uniquely present in Directory2 it has to be deleted.
If the file is present in both Directory1 and Directory2 , files in Directory1 has to be copied to Directory2 with the same folder structure [Updates].
Simply use diff, eg :
diff -r dir1 dir2 | grep dir1
Only in dir1: file2
Only in dir1/subdir1: file4
Only in dir2/subdir1: file6
You can then awk, or store the result in a temporary file and use it in your script.
You seem to talk about a mirroring function, see the nice Open Source tool rsync.
https://rsync.samba.org/
It can do all of that and even more (also remote synchronization via LAN or via SSH if needed).
rsync -options --otherOptions sourceDir targetDir
Usually you would use these commandline options:
rsync -av /src/foo /dest
or
rsync -av /src/foo/ /dest/foo
Note: if you omit the trailing "/" of /src/foo, then rsync will mirror to /dest and create a foo subdir. You have either the one or the other choice how to use this command.

Linux command to find the difference between two folders

I have two folders, each with sub folders, and I want to see if there are any sub folders in one file that does not exist the other folder. I have tried this command:
diff -r file1 file2
but it does not provide the results that I want.
For example if file1 contains three folders A, B, and C and file 2 contains 1 folder B, then the output should be folders A and C.
diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt
Explanation:
diff -r dir1 dir2 shows which files are only in dir1 and those
only in dir2 and also the changes of the files present in both directories if any.
diff -r dir1 dir2 | grep dir1 shows which files are only in dir1
awk to print only filename.

Using diff command to find difference between two directories

I wanna get changed parts in .diff files between the modifiied and the original kernel files.
I use diff -b -r -w dir1 dir2
it gives it on command line but I wanna get it in seperate diff extension files.
diff -b -r -w dir1 dir2 >> your_file_name.patch
will save your result.

cp command failing in Linux

I am facing a copy command problem while executing shell script in RHEL 5.
executed command is
cp -fp /fir1/dir2/*/bin/file1 `find . -name file1 -print`
error is
cp: Target ./6e0476aec9667638c87da1b17b6ccf46/file1 must be a directory
Would you please throw some ideas why it would be failing?
Thanks
Robert.
When cp is called with more than two filenames as arguments, it treats the last one as a target directory, and copies all the files named in the other arguments into that target directory. So, for example,
cp file1 file2 dir3
will create dir3/file1 and dir3/file2. It seems that in your case, the pattern /fir1/dir2/*/bin/file1 matches more than one filename, so cp is trying to treat the result of find as a target directory - which it isn't - and failing.
You can't copy many files to one location unless that location is a directory.
cp should be used thusly: cp sourcefile destinationfile or cp source1 source2 destinationdir.
As the others said you cannot copy multiple files to one file using cp. On the other hand, if you want to append the content of multiple files together into one destination file you can use cat.
For instance:
cat file1 file2 file3 > destinationfile
it is hard to answer without knowing what you are trying to achieve.
If, for example, you want to copy all files named "file1" within a directory structure to a target place /tmp, building the same directory structure there, this command will do the trick:
cd /dir1/dir2
find . -name file1 | cpio -pvd /tmp
You cannot copy multiple multiple files to a file, only to a directory, i.e.
cp file1 file2 file2 file4
is not possible, you need
cp file1 file2 file2 dir1

Resources