#!/bin/bash # # fixes folder name encoding by checking size of folder: # fixfile.sh file* # # e.g fixfile.sh 03.\ Needs\ M* # # how we check which folder is most likely to be the right one: compare `du` output and the one that's largest is most likely to be the right one - if at least one of the folders is not empty or at least marginally small (less than 2% of the largest folders size), that means that we have selected the wrong folder calc() { [ $# != 1] && echo "arg error in calc()" && exit 1 return "echo "scale=4; $1" | bc" } if [ $# != 2 ]; then echo "usage: $0 folder1 folder2" echo " the script will automagically find the folder with the correct contents" echo " tip: use shell wildcards, e.g fixfile.sh 03.*" exit fi export FILEWITHCONTENT export FILEINCORRECTNAME FILEONESIZE=`du "$1" | awk '{ print $1 }'` FILETWOSIZE=`du "$2" | awk '{ print $1 }'` if [ $FILEONESIZE -gt $FILETWOSIZE ]; then # first argument is the right one FILEWITHCONTENT=$1 FILEINCORRECTNAME=$2 BIGFOLDER=$FILEONESIZE SMALLFOLDER=$FILETWOSIZE elif [ $FILEONESIZE -lt $FILETWOSIZE ]; then FILEWITHCONTENT=$2 FILEINCORRECTNAME=$1 BIGFOLDER=$FILETWOSIZE SMALLFOLDER=$FILEONESIZE else # else $FILEONESIZE -eq $FILETWOSIZE echo "the folders have the same size, unsure what to do!" exit 2 fi let MULTIPLICATION=$SMALLFOLDER*50 [ $BIGFOLDER -lt $MULTIPLICATION ] && echo "the largest folder isn't at least 50 times bigger than the smallest folder, not touching anything!" && exit 2 [ "$FILEWITHCONTENT" == "" ] && [ "$FILEINCORRECTNAME" == "" ] && echo "logic error somewhere in the script" && exit echo "Folder that has content: $FILEWITHCONTENT" echo "Folder that has incorrect name: $FILEINCORRECTNAME" TRASHFOLDER=/var/tmp/trash mkdir -p $TRASHFOLDER mv "$FILEINCORRECTNAME" $TRASHFOLDER && mv "$FILEWITHCONTENT" "$FILEINCORRECTNAME" && echo "done" && exit exit 1