Click to See Complete Forum and Search --> : Shell script to get difference of two directories
matrixonline
01-31-2004, 05:12 AM
Hey Guys,
I am new to shell script. I cant solve this problem. Please help.
Q. Write a shell script to accept two argument as input and display differences of files between two directories in "outfile".
Regards
JohnT
01-31-2004, 06:31 AM
Originally posted by matrixonline
Hey Guys,
I am new to shell script. I cant solve this problem. Please help.
Regards
Post what you have tried.
Here's one of the best references on the web for bash scripting (in the process of being updated) its the old one but stiil applicable.
http://www.freeos.com/guides/lsst/
matrixonline
02-01-2004, 06:34 AM
I have tried diff -u option to diffrentiate two files and can take argument in the form of:
scriptname <argument1> <argument2>
and the diff -u $1 $2
But i cant move further..
Help...
I tried the link, it is very much helpfull.
Regards,
bwkaz
02-01-2004, 03:17 PM
Redirection is your friend. If you can get something to display on the screen, you can get bash to store it into a file (as long as you have write permission to the file, or write permission to the directory that the file's in if the file doesn't exist).
I won't give you the answer directly (this does sound a lot like an assignment...), but I will say to look around for tutorials or reference material on redirection.
matrixonline
02-02-2004, 04:45 AM
I have used pipelines to redirect the output to the "outfile" ..But can you give some more options regarding "diff". Iv tried man on diff but cant go beyond. Thanx anyway..
chrism01
02-02-2004, 01:50 PM
From the way the qn is phrased, mentioning 2 dirs, it sounds more like they want the diff of running 'ls' on each dir, rather than the difference in contents of the files in each dir.
Its not entirely clear tho'.....
matrixonline
02-06-2004, 09:18 AM
Actually i want know if it will be possible to find differences of all files in 2 distinct "dirs".
Suppose they have same set of files, but files in one dir is modified and other remains unchanged. if all files are tracked about their modification(by showing their difference) then it may work as a log. Is it possible?
matrixonline
02-07-2004, 03:04 AM
Guide me how ?
chrism01
02-11-2004, 06:02 PM
hint: man diff
better: man sdiff
:-)
The basic idea is to generate a list of all filenames in dir1, then loop through the list, diff'ing the corresponding file in dir2.
Ideally you should allow for not all files existing in both dirs (unless otherwise specified).
Save the diffs for each file-pair in a separate file for ease of reading/using later eg
file1.diff
file2.diff
etc...
nowonmai
02-12-2004, 07:24 AM
hint, hint... using "for" on the dir listing files will allow you to perform an operation on each line in the file.
bwkaz
02-12-2004, 07:32 PM
If you're writing it in csh...
In which case, I would ask that you Google for "csh programming considered harmful" and read it... ;)
X_console
02-13-2004, 01:51 AM
So something like: scriptname dir1 dir2 would give the difference? I suppose a possible algorithm could be:
# this is untested but you get the idea
function getDiff()
{
cd ${currentDir};
for i in *; do
found=$(ls ${2} 2>/dev/null 1>/dev/null);
if [ ${?} -eq 1 ]; then
echo ${i} >> ${outfile};
fi
done
}
outfile="outfile.txt";
currentDir=${1};
getDiff();
currentDir=${2}
getDiff();
matrixonline
02-20-2004, 09:06 AM
Well friends,
thanks for the help. I am almost done with the problem....