DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Compare Two File Lists In Bash

02.25.2010
| 5546 views |
  • submit to reddit
        // This will compare two lists of files in bash fairly efficiently without using diff or grep etc.

#!/bin/bash

SVN_FILES="::$(svn ls http://svn.domain.com:3690/svn/repo/path/to/files|tr '\n/' ':')"
DIR_FILES=":$(ls /path/to/files/|tr '\n' ':')"

for DIR_FILE in ${DIR_FILES//:/ }
do

    for SVN_FILE in ${SVN_FILES//::/ }
    do

        if [ "${DIR_FILE}" = "${SVN_FILE}" ]
        then
            : echo "YES ${DIR_FILE} ${SVN_FILE}"
            SVN_FILES=${SVN_FILES/:${SVN_FILE}:/}
            DIR_FILES=${DIR_FILES/:${DIR_FILE}:/:}
            continue
        else
            : echo "  no ${DIR_FILE} ${SVN_FILE}"
        fi

    done

done

for DIR_FILE in ${DIR_FILES//:/ }
do
    echo "Left: file [$DIR_FILE]"
done

for SVN_FILE in ${SVN_FILES//::/ }
do
    echo "Left: svn [$SVN_FILE]"
done