Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hie there,
i would like to know how do i compare files in a current directory? i don't want to compare just 2 files with the diff filename1 filename2. i need to compare all the files in the current directory to check if there is any identical file. and then if it's identical report the filenames that are identical. i found this script from this site that compare all files with a filename called master. can anyone modify this so it compare all files and not just filename master?for fn in `ls`
do
if [ -f $fn -a -r $fn -a $fn != master ]
then
if diff master $fn > /dev/null 2>&1
then
echo "identical to master: $fn"
fi
fi
donehttp://www.computing.net/unix/wwwboard/forum/2694.html

i've finally got an idea that is to use loop. i'm a newbie in unix. so can anyone help me with the script?

The 'for fn...' loop lists all of the files so that they can be compared with the file master. What you want is that instead of master, the second file in the compare goes thru all of the files also. So...
put another loop around the outside of this code
for master in `ls` # this is a new line
do # this is a new linefor fn in `ls`
do
if [ -f $fn -a -r $fn -a $fn != master ]
then
if diff master $fn > /dev/null 2>&1
then
echo "identical to master: $fn"
fi
fi
donedone # this is a new line
Now, master is a variable rather than a given filename, so other than the first line, change all other occurrences of master to display the value of the variable master.for master in `ls` # this is a new line
do # this is a new linefor fn in `ls`
do
if [ -f $fn -a -r $fn -a $fn != $master ] # changed line
then
if diff $master $fn > /dev/null 2>&1 # changed line
then
echo "identical to $master: $fn" # changed line
fi
fi
donedone # this is a new line
Note that this simple way is going to cause a lot of file comparisons (n^2) and a lot of unnecessary comparisons. For example, if you files are named file1, file2, file3, file4, this method will compare file1 to file1, file2 to file2, etc. Also, it will compare file1 to file2 and then later, file2 to file1, so it's comparing the same files again.
So, actually, this isn't a very good method!!

Depends what you want this for.
You could use "cksum * | sort" as your initial input, then take each line in turn and if the first two columns (ie. checksum and characters) are identical to the previous link them.
Sean

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |