Script to find file differences in two directory trees (bash)
I'm new to bash scripting, and I'll try to describe my problem as succinctly as possible.
I have two directory trees, which have the same structure. These trees go 6 or 7 levels deep, with a mixture of files/folders at every level (until the final level obviously). Let's call these trees dir1 and dir2.
I want to write a script that greps all the files in dir1 to their counterpart in dir2, and then put all the individual file grep results into a third folder, say /grepped.
If a file exists in dir1, but not in dir2 (or vice versa), then a grepped file should exist in /grepped with just the line "/dir1/foo/fileName.txt does not exist in /dir2" - or something equivalent.
So how do I traverse the directory tree? I can use test -d to figure out if something is a path or directory, but then I'm not sure how to actually move around the tree.
Additionally, I searched google and this site for a solution to anything similar to this and was unable to find it. If someone has a good link, please send it my way.
hey, it's quite easy. forget about the tree, treat it as a text, line by line.
just create your dir1.txt and dir2.txt by running find ./ > dir1.txt having working directory dir1 and do the same for dir2.
than you can do something like
cat dir1.txt | while read line
grep "$line" dir2.txt >> grepped.txt
I guess you know what to do next, let me know if you need additional help
Here is what I actually settled on. I know it's very rough, but it works so far! Still trying to figure out how to use say in code "if you don't find the file in the first directory, and it is present in the second directory". That's why you see the hack with echo stuff > /dev/null.
Also, I need to ignore all temporary files.
Does the basis of diff -r not provide what you need?
It tells you:
a) which files exist in only one or the other directory tree
b) the differences between two corresponding files.
You can also use the -q option to give you easier parsing:
Diff has plenty of good options - be sure to review the man page.
|All times are GMT -5. The time now is 08:25 PM.|