#!/bin/bash # This script leaves one of duplicated files unchanged and changes the other duplicated files to symbolic links to the left file. # The modified time of the generated symbolic link is adjusted to the round-up minutes of the modified time of its original file (not the left file). # The name of the generated symbolic link is that of its originial file (not the left file). # Written by Kiyoshi SUZUKI ("kiyop" in Debian forum and Ubuntu forum and Ubuntu Japanese Team Forum) scroll () { a=0 while [ $a -lt $1 ];do echo a=$(expr $a + 1) done } width=$(dialog --print-maxsize 2>&1|head -n1|cut -d " " -f3) height=$(dialog --print-maxsize 2>&1|head -n1|cut -d " " -f2|tr -d ,) if [ -e fdupes2.log ];then scroll $height dialog --msgbox "This script will delete fdupes2.log. Please use this script on the directory which does not contain fdupes2.log" $height $width exit 1 fi if [ -e fdupes2.log2 ];then scroll $height dialog --msgbox "This script will delete fdupes2.log. Please use this script on the directory which does not contain fdupes2.log2" $height $width exit 1 fi if [ -n "$(pwd|grep ' ')" ];then scroll $height dialog --msgbox "This script cannot work correctly on a directory the name of which involves space." $height $width exit 1 fi makelink () { # $1 must not be a directory, but must be a file. # $2 must not be a directory, but must be a file. # $1 is the path (name) of a destination file of the symbolic link to be generated. # $currentdir is the full path of the current directory. # $2 is the path (name) of the symbolic link to be generated. savedir=$(echo $2|sed -e "s@[^/]*\$@@") if [ -z "$savedir" ];then savedir="$currentdir";fi savedir=$(readlink -e "$savedir") if [ -n "$(echo "$savedir"|tr "/" '\n'|tail -n1)" ];then savedir="${savedir}/" fi cd "$savedir" savedir1=$savedir savedir1=$(echo $savedir1|cut -c2-) link=$(echo "${currentdir}/${1}") link=$(readlink -e "$link"|cut -c2-) until [ a"$(echo $link|grep \/)" = "a" -o a"${savedir1}" = "a" ];do if [ a"$(echo $link|sed -e 's@/@\n@'|head -n 1)" = a"$(echo $savedir1|sed -e 's@/@\n@'|head -n 1)" ];then num=$(expr $(echo $link|sed -e "s@/@\n@"|head -n 1|wc -c) + 1) link=$(echo $link|cut -c${num}-) savedir1=$(echo $savedir1|cut -c${num}-) else break fi done if [ a"$savedir1" = "a" ];then num=0 else num=$(echo $savedir1|sed -e "s@/@\n@g"|wc -l) fi while [ $num -gt 1 ];do link="../$link" num=$(expr $num - 1) done echo "link is tried to be made for $link with the name $(basename $2) in $savedir" >> $logfilename linkname=$(basename "$2") ln -s "$link" "$linkname" if [ $? = 1 ];then scroll $height dialog --title "" --msgbox "${savedir}/${linkname} cannot be made.\nMaybe there is not write permission of ${savedir}.\nPlease select a directory with write permission.\nThe link is not generated." $height $width echo "${savedir}/${linkname} cannot be made.\nMaybe there is not write permission of ${savedir}.\nPlease select a directory with write permission.\nThe link is not generated." >> $logfilename # Return to original directory after the above task finished. cd "$currentdir" exit 1 else echo "The above is succeeded." >> $logfilename fi cd "$currentdir" } currentdir="$PWD" echo $currentdir logbegindate="$(date +%C%y%m%d%k%M|tr -d ' ')" logfilename="$currentdir/fdupes.log_$logbegindate" touch "$logfilename" fdupes -rn . > fdupes2.log2 echo "The below is the result of \"fdupes -rn .\" at $(pwd) at $logbegindate =====" >> $logfilename cat fdupes2.log2 >> $logfilename sed -e "s@^./@@" fdupes2.log2 > fdupes2.log rm fdupes2.log2 logfilename="$currentdir/fdupes2.log_$logbegindate" touch $logfilename echo "The below is the log of fdupes2 at $(pwd) at $logbegindate =====" >> $logfilename while [ $(wc -l fdupes2.log|cut -d " " -f1) -gt 1 ];do dealtlines="$(sed -e "/^$/q" fdupes2.log|grep -v ^$)" if [ -z "$dealtlines" ];then mv fdupes2.log fdupes2.log2 sed -e "1d" fdupes2.log2 > fdupes2.log rm fdupes2.log2 continue fi echo "" >> $logfilename if [ -n "$(echo "$dealtlines"|grep ' ')" ];then scroll $height dialog --msgbox "This script cannot deal files with full path involving ' '. Skip $dealtlines" $height $width echo "This script cannot deal files with full path involving ' '. The following files are skipped. $dealtlines" >> $logfilename mv fdupes2.log fdupes2.log2 num=$(echo "$dealtlines"|wc -l|cut -d " " -f1) num=$(expr $num + 1) sed -e 1,${num}d fdupes2.log2 > fdupes2.log rm fdupes2.log2 continue fi strings=$(echo "$dealtlines"|sed -n -e = -e p) scroll $height selected=$(dialog --no-shadow --colors --stdout --menu "Select the file to keep. The others will be changed to symbolic links" $height $width 10 $strings) if [ $? = 1 ];then echo "The following files are skipped according to the request. $dealtlines" >> $logfilename mv fdupes2.log fdupes2.log2 num=$(echo "$dealtlines"|wc -l|cut -d " " -f1) num=$(expr $num + 1) sed -e 1,${num}d fdupes2.log2 > fdupes2.log rm fdupes2.log2 continue fi originalfile=$(echo "$dealtlines"|sed -n -e ${selected}p) dealtlines=$(echo "$dealtlines"|grep -v ^$originalfile\$) for dealt in $dealtlines;do time=$(ls --full-time $dealt|cut -d " " -f 6-7|tr -d " "|tr -d :|tr -d -|sed -e "s/\..*//") time=$(echo $time|cut -c 1-12) filedata=$(ls -l $dealt) rm $dealt if [ $? = 1 ];then scroll $height dialog --title "" --msgbox "$dealt cannot be deleted.\nMaybe we do not have write permission of it.\nPlease select a directory with write permission.\nThe link is not generated." $height $width echo "$dealt cannot be deleted.\nMaybe we do not have write permission of it.\nPlease select a directory with write permission.\nThe link is not generated. --" >> $logfilename continue else echo "The following file is deleted. $filedata" >> $logfilename fi makelink "$originalfile" "$dealt" touch -c -h -t ${time}.60 $dealt if [ $? = 1 ];then echo "The modified time of $dealt is failed to be changed." >> $logfilename else echo "The modified time of $dealt is successfully changed to ${time}.60" >> $logfilename fi # The minutes is round up so that the original file is overwritten by the generated symbolic link if "cp -au" is used to synchronize. echo "--" >> $logfilename done mv fdupes2.log fdupes2.log2 num=$(echo "$dealtlines"|wc -l|cut -d " " -f1) num=$(expr $num + 1) sed -e 1,${num}d fdupes2.log2 > fdupes2.log rm fdupes2.log2 done rm fdupes2.log exit 0