Created
May 15, 2012 10:49
-
-
Save alexandruc/2700790 to your computer and use it in GitHub Desktop.
merge and check folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#checks and merges 2 folders | |
output_file="output.txt" | |
sep_chr="----" | |
c_dir=$( pwd ) | |
n_args=2 #minimum number of args | |
mmerge=0 | |
#some colors | |
text_green=$(tput setaf 2) | |
text_red=$(tput setaf 1) | |
text_reset=$(tput sgr0) | |
#print usage | |
function usage { | |
echo Usage: | |
echo "./check_delivery.sh [OPTION] <dir1> <dir2>" | |
echo OPTION= | |
echo " -m --merge = create new merged directory, with merged directories (newer content only)" | |
echo " -h --help = prints usage information" | |
} | |
#total number of files | |
# $1 dir1 | |
# $2 dir2 | |
function num_files { | |
local numfiles1=$( find $1 -type f | wc -l ) | |
local numfiles2=$( find $2 -type f | wc -l ) | |
local numdirs1=$( find $1 -type d | wc -l ) | |
local numdirs2=$( find $2 -type d | wc -l ) | |
echo Total dirs: $numdirs1 $sep_chr $numdirs2 >>$output_file | |
echo Total files: $numfiles1 $sep_chr $numfiles2 >>$output_file | |
echo >>$output_file | |
echo >>$output_file | |
} | |
#check merged files | |
# $1 - dir1 | |
# $2 - dir2 | |
#Checks: | |
#file name | |
#file size: stat --format=%s $file | |
#last mod date: stat --format=%Y $file | |
#file type: file -b $file | |
function merge_check | |
{ | |
echo -n Checking files from $1 | |
local count=0 | |
local errors=0 | |
cd $c_dir | |
cd $1 #set first dir as current dir -> to get same path (path prefixes might differ) | |
for file1 in $( find -type f ); do | |
let "count += 1" | |
#get check data for first file | |
filename1=$(basename $file1) | |
#weird error on stat... | |
#filedate1=$(stat --format=%Y $file1) | |
filetype1=$(file -b $file1) | |
cd $c_dir | |
cd $2 #check with second folder | |
file2=$(find -type f | grep $file1) | |
if [[ "$file2" == "" ]]; then | |
echo | |
echo "$text_red $file1 is in $1, but is MISSING from merged folder $text_reset" | |
let "errors += 1" | |
continue | |
fi | |
#get check data for second file | |
filename2=$(basename $file2) | |
#filedate2=$(stat --format=%Y $file2) #don't use for now | |
filetype2=$(file -b $file2) | |
#check for equality | |
#echo Checking $file1 against $file2 | |
echo -n . | |
if [[ "$filename1" != "$filename2" || "$filetype1" != "$filetype2" ]]; then | |
echo "$text_red $file1 --IS DIFFERENT-- $file2 :" | |
echo " $filename1 ---- $filename2" | |
echo " $filetype1 ---- $filetype2 $text_reset" | |
let "errors += 1" | |
fi | |
done | |
echo | |
echo -n "Checked $count ->" | |
if [[ $errors != 0 ]]; then | |
echo "$text_red ERRORS were found $text_reset" | |
else | |
echo "$text_green NO ERRORS $text_reset" | |
fi | |
cd $c_dir #back to default dir | |
} | |
#main flow | |
if [[ $# < $n_args ]]; then | |
usage | |
exit 1 | |
fi | |
while [ "$1" != "" ]; do | |
case $1 in | |
-m | --merge ) shift | |
#do merge copy | |
mmerge=1 | |
;; | |
-h | --help ) usage | |
exit | |
;; | |
* ) break | |
esac | |
done | |
echo Comparison of contents dir1=$1 dir2=$2 >$output_file | |
num_files $1 $2 | |
echo diff result: >>$output_file | |
echo >>$output_file | |
echo >>$output_file | |
diff -ry --brief $1 $2 >>$output_file | |
#merge files (keep newer) | |
if [[ $mmerge -eq 1 ]]; then | |
echo Merging dirs | |
if [ -d merged ]; then | |
rm -r merged/* | |
rm merged | |
fi | |
mkdir merged | |
cp -vfudpr $1/* merged/ | |
cp -vfudpr $2/* merged/ | |
echo "Merge completed -> check $(pwd)\\merged directory" | |
echo Checking merge: | |
merge_check "$1" "merged" | |
merge_check "$2" "merged" | |
#check merged files/dirs | |
cd "merged" | |
ndirs=$(find -type d | wc -l) | |
nfiles=$(find -type f | wc -l) | |
echo "Merged dirs: $ndirs" | |
echo "Merged files: $nfiles" | |
cd $c_dir | |
echo >>$output_file | |
echo "Merged dirs: $ndirs" >>$output_file | |
echo "Merged files: $nfiles" >>$output_file | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment