Computing.Net > Forums > Unix > Comparing all files in current dir

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Comparing all files in current dir

Reply to Message Icon

Name: Kurt Tan
Date: October 27, 2002 at 07:15:30 Pacific
OS: Unix
CPU/Ram: 256
Comment:

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
done

http://www.computing.net/unix/wwwboard/forum/2694.html



Sponsored Link
Ads by Google

Response Number 1
Name: Kurt Tan
Date: October 27, 2002 at 07:41:10 Pacific
Reply:

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?


0

Response Number 2
Name: Don Arnett
Date: October 27, 2002 at 13:41:55 Pacific
Reply:

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 line

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
done

done # 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 line

for 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
done

done # 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!!


0

Response Number 3
Name: Sean Miller
Date: October 28, 2002 at 03:45:41 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Comparing all files in current dir

Delete all files in sub-dir +10 old www.computing.net/answers/unix/delete-all-files-in-subdir-10-old/7166.html

find files in current dir ONLY www.computing.net/answers/unix/find-files-in-current-dir-only/4800.html

execute shell file in other dir www.computing.net/answers/unix/execute-shell-file-in-other-dir/4754.html