[SlugBug] shell script help

Eric E Moore e.e.moore at sheffield.ac.uk
Thu Aug 7 19:59:21 BST 2003


Bill Best <bill at commedia.org.uk> writes:

> many thanks for everyone's help with this.  the definitive script
> (apart from daniel's that i've just seen) is thus which works a treat:
>
> for FILE in $(ls directory1/*); do
>  for FILE2 in $(ls directory2/*); do
>    cmp -s $FILE $FILE2
>     [ $? -eq 0 ] && echo $FILE $FILE2
>     done
> done

this can, for reference, be simplified to:

for FILE in directory1/* 
do
  for FILE2 in directory2/*
  do
    if ! cmp -s $FILE1 $FILE2
    then 
       echo $FILE1 $FILE2
    fi
  done
done


[ is actually a program, if you're using return status of another
command, you can use that command in the if.

since it's the shell that does * expansion, there's no need to do the
$(ls directory/*), which gets expanded into the list of files before
it gets passed to ls.

if the number of files in each directory is large, and you're willing
to risk a less than a one and a trillion chance of reporting two files
are the same, you can instead use:

find dir1 dir2 -type f -maxdepth 1 | xargs md5sum | sort | uniq -w 32 -D

(although it will report on files that are identical in the same
directory as well, don't know if that's a problem, it will be much
faster if the number of files is large :)

-- 
Eric E. Moore
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 184 bytes
Desc: not available
Url : http://www.email-lists.org/pipermail/slugbug/attachments/20030807/88b82746/attachment.bin


More information about the SlugBug mailing list