How do I use bash to compare 1000 files in sequential order - 1 to 2, 2 to 3, and so on
I have about 1000 html files in a directory that are different versions of a webpage. Each is named by the date the page was modified - e.g. 20190104
, 20190105
- but there are occasional gaps, such as between 20190105
and 20190107
.
I know how to use diff to compare one file to the immediately subsequent version and save the differences to a third file, but I want a loop that will that for all 1000. The standard for i in ./*
doesn't work because I need to reference multiple files inside the loop.
I suspect I need to turn the list of files into an array, and then have a for loop that references array elements n and n+1, but I'm having trouble with the syntax.
elements n and n+1
No, don't think in the future - you don't know it. You know the past - iterate over n-1
and n
.
prev= # empty previous filename
for cur in ./*; do
if [[ -n "$prev" ]]; then # if prev is Nonempty
do_stuff "$prev" "$cur"
fi
prev=$cur
done
Comparing by pairs:
#!/usr/bin/env sh
comparePairs() {
while [ $# -gt 1 ]
do
printf 'Compare %s with %s.\n' "$1" "$2"
shift
done
}
comparePairs foo bar baz cuux
output:
Compare foo with bar.
Compare bar with baz.
Compare baz with cuux.
Now your diff pairs:
#!/usr/bin/env bash
diffPairs() {
outputDir="$1"
shift
while [ $# -gt 1 ]
do
diff "$1" "$2" > "$outputDir/$1-$2.diff"
shift
done
}
# Use first argument as output directory
diffPairs "$@"
Example:
./diffPairs ~/outputdir ./*.html