diff --git a/updatehash.sql b/updatehash.sql index 08ffee5..2a226ea 100644 --- a/updatehash.sql +++ b/updatehash.sql @@ -6,3 +6,20 @@ select size,path from files where md5||'#'||sha1||'#'||size in (select md5||'#'| # Total count of files and total weight in Gb select round(sum(size)/(1024.*1024.*1024.),2)||' Gb '||count(size)||' files' from files; + +# find files not in folder A which have a duplicate in folder A. +update files set tag = 'A' where path like './A/%'; +create table hashesA(id, hash); +insert into hashesA select rowid,size||'#'||md5||'#'||sha1 from files where tag == 'A'; +create table hashesother(id, hash); +insert into hashesother select rowid,size||'#'||md5||'#'||sha1 from files where tag != 'A'; +create index i_hashesA_hash on hashesA(hash); +create index i_hashesother_hash on hashesother(hash); +# find files not in folder A which have a duplicate in folder A. +select (select path from files where rowid == hashesother.id) from hashesother where hashesother.hash in (select hash from hashesA); +# find files not in folder A associated with one of their duplicates in folder A. +select (select path from files where rowid == hashesother.id),(select (select path from files where rowid == hashesA.id) from hashesA where hashesA.hash == hashesother.hash) from hashesother where hashesother.hash in (select hash from hashesA); + +# Rename (prepend ".% to file name) files not in folder A which have a duplicate in folder A. +[ -e hashes.db ] && sqlite3 hashes.db "select (select path from files where rowid == hashesother.id) from hashesother where hashesother.hash in (select hash from hashesA);" > dup.lst +pv -l dup.lst | while read ab; do file="${ab##*/}"; dir="${ab%/*}"; dest="${dir}/.%${file}"; if [ -e "$ab" ]; then [ "$file" != "${file#.%}" ] || [ -e "$dest" ] || mv -i "$ab" "$dest"; fi; done