comp:deldup
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
comp:deldup [2008-08-16 1353] – angelegt werner | comp:deldup [2010-11-03 1255] (aktuell) – Dateiname zum einfacheren Download angegeben werner | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== Duplikate eliminieren ====== | ||
+ | Es kann durchaus mal vorkommen, dass man ein und dieselben Daten in verschiedensten Verzeichnissen auf der Platte hat. Sowas haut die Platte voll, ohne dass wirklich mehr Info zusammen kommt ;-) | ||
+ | |||
+ | Der erste Ansatz ist natürlich, die Duplikate zu löschen. Aber manchmal hat man die Dateien nach verschiedenen Kriterien sortiert, und es wäre unangenehm, wenn die Daten in der einen Sortierung fehlten. Deshalb habe ich mir ein Script ausgedacht, das die doppelten Dateien löscht und als Default-Aktion einen Hardlink zum Original erstellt -- die Ordnung bleibt erhalten, aber es wird weniger Platz verbraucht. Per Parameter lässt sich das abstellen bzw. zum Symlink ändern. | ||
+ | |||
+ | <code bash findup.sh> | ||
+ | #!/bin/bash | ||
+ | ######################################## | ||
+ | # | ||
+ | # find duplicate files in a given directory | ||
+ | # | ||
+ | # written 2008-05-17 | ||
+ | # modified for subdir handling 2008-08-10 | ||
+ | # added parameter handling 2008-08-16 | ||
+ | # | ||
+ | ######################################## | ||
+ | # set -x | ||
+ | |||
+ | # default values for all operations | ||
+ | ACTION=' | ||
+ | LNPARAM=' | ||
+ | PATTERN=' | ||
+ | DEPTH=1 | ||
+ | SPEAK=1 | ||
+ | E_OK=0 | ||
+ | |||
+ | # why starting the getopts argument string with a colon? | ||
+ | # to answer, please read 'man getopts' | ||
+ | while getopts ": | ||
+ | case $Option in | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | # now we look at the other parameters (if any) | ||
+ | shift $(($OPTIND - 1)) | ||
+ | |||
+ | # if we still have a parameter, this will be the starting directory | ||
+ | # if the parameter is not given, it defaults to the current dir | ||
+ | DIR=${1-$(pwd)} | ||
+ | # make sure we have a trailing slash | ||
+ | echo " | ||
+ | |||
+ | # temporary files | ||
+ | OUTFILE=" | ||
+ | test -f " | ||
+ | SORTFILE=" | ||
+ | test -f " | ||
+ | |||
+ | function show_usage() { | ||
+ | echo 'This bash script tries to eliminate duplicate files in a directory.' | ||
+ | echo 'En passant, it removes the " | ||
+ | echo ' ' | ||
+ | echo " | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' | ||
+ | echo ' ' | ||
+ | } | ||
+ | |||
+ | function doonedir() { | ||
+ | HELPFILE=" | ||
+ | while read DAT ; do | ||
+ | MYDAT=" | ||
+ | test -x " | ||
+ | MYMD5=$(md5sum | ||
+ | MYSHA=$(sha1sum -b -- " | ||
+ | echo " | ||
+ | done < " | ||
+ | } | ||
+ | |||
+ | # get all subdirs to current dir | ||
+ | DIRLIST=$( find " | ||
+ | |||
+ | for WDIR in $DIRLIST ; do | ||
+ | # get all files in the current dir | ||
+ | # Problem: may be too many files, so better store them in a help file | ||
+ | WODIR=$(echo " | ||
+ | HELPFILE=" | ||
+ | test -f " | ||
+ | |||
+ | # just search in the current directory, don't use $DEPTH here! | ||
+ | find " | ||
+ | # now loop through the resulting file and compute MD5sum and SHA1sum | ||
+ | # again, the result is put into a file | ||
+ | doonedir " | ||
+ | |||
+ | rm -f -- " | ||
+ | test $SPEAK -ge 1 && echo " | ||
+ | done | ||
+ | |||
+ | # sort the resulting file on MD5sum | ||
+ | sort <" | ||
+ | rm -f -- " | ||
+ | |||
+ | # walk through the sorted file and compare MD5sums/ | ||
+ | # when they are equal, echo this and delete one of them | ||
+ | OLD_FL=" | ||
+ | OLD_MD=" | ||
+ | OLD_SH=" | ||
+ | DELCOUNT=0 | ||
+ | while read MD SH FL ; do | ||
+ | if [ " | ||
+ | test $SPEAK -eq 2 && echo "same MD5sum/ | ||
+ | rm -f -- " | ||
+ | test " | ||
+ | else | ||
+ | OLD_MD=" | ||
+ | OLD_SH=" | ||
+ | OLD_FL=" | ||
+ | fi | ||
+ | done < " | ||
+ | rm -f -- " | ||
+ | echo " | ||
+ | exit $E_OK | ||
+ | </ | ||
+ | |||
+ | Ein dickes Dankeschön an Mendel Cooper dafür, dass er den großartigen [[http:// |