#!/bin/bash # $Id: oddb_renlst 2 2008-03-28 15:52:26Z winex $ SIZE_ONLY=0 ### # TODO: 20070924 winex: make getting list of files by args to script # and checking them with 'stat -c "%s" $arg' ### if [ "$1" != "" ]; then lst=$1 fi # cat lst to tbl, replacing all spaces with '_SPACE_' tbl=( $(cat "$lst" | sed 's/ /_SPACE_/g') ) # for each 3rd entry do the magic :) for i in $( seq 0 3 $((${#tbl[@]} - 1)) ) do md5=${tbl[$i]} size=${tbl[$i+1]} name=${tbl[$i+2]} cmd_out="" result=0 # TODO: 20070924 winex: check if size == 0 #found=$(find . -size "$size"c -printf "%P\n") found=$(find . -maxdepth 1 -size "$size"c -printf "%f\n") if [ "$found" != "" ]; then name=$(echo "$name" | sed 's/_SPACE_/ /g') bname=$(basename "$name") # need below? printf "found size match of: \n" printf "\tmd5: $md5\tsize: $size\t'$name'\n" for ent in $(echo "$found" | sed 's/ /_SPACE_/g'); do ent=$(echo "$ent" | sed 's/_SPACE_/ /g') #printf "\t\t\t$ent\n" if [ -f "$ent" ]; then if [ $SIZE_ONLY -eq 0 ]; then md5_2=$(md5sum "$ent" 2>/dev/null | cut -b-32) if [ "$md5_2" != "" ]; then printf "\tmd5: $md5\tsize: $size\t$ent\n" #no need to get its size, we'll rely on 'find' if [ "$md5_2" == "$md5" ]; then printf "\tCHECKSUM EQUAL, renaming '$ent' to '$bname'..." cmd_out=$(mv "$ent" "$bname" 2>&1) result=$? fi fi else #printf "\tmv \"$ent\" \"$name\"\n" printf "\tSIZE EQUAL, renaming '$ent' to '$bname'..." cmd_out=$(mv "$ent" "$bname" 2>&1) #printf "\tSIZE EQUAL, renaming '$ent' to '$name'..." #cmd_out=$(mv "$ent" "$name" 2>&1) result=$? fi fi if [ $result -eq 0 ]; then echo "OK" else printf "\nERROR: '" echo -n "$cmd_out" echo "'" fi done fi done echo # script_contents=( $(cat "$lst") ) # Stores contents of this script ($lst) # #+ in an array. # # for element in $(seq 0 $((${#script_contents[@]} - 1))) # do # ${#script_contents[@]} # #+ gives number of elements in the array. # # # # Question: # # Why is seq 0 necessary? # # Try changing it to seq 1. # echo -n "${script_contents[$element]}" # # List each field of this script on a single line. # echo -n '\t' # Use " -- " as a field separator. # done #for ent in $(cat "$lst" | sed 's/ /__/g' | sed 's/\([\\'']\)/\\&/g'); do #{ # #k=`echo '$ent' | awk -F\t '{print $2;}'` # echo "$ent" # #`find . -maxdepth 1 -size $k"c"` # #basename $k #} #done #cat $lst | awk -F'\t' '{ print $1" "$3; }' | md5sum -c #find $path_arg -name "$name_arg" -type f -printf "%s\t" -exec md5sum -b {} \; | \ #find . -type f -printf "%s\t" -exec md5sum -b {} \; | \ #sed 's/ [ \*]/\t/g' | sed 's/\.\///' | sed 's/\([0-9]*\t\)\([0-9a-f]*\t\)/\2\1/'