Advertisement
RedTeaHacker

Rename Images to MD5

Dec 3rd, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.34 KB | None | 0 0
  1. #!/bin/sh
  2. # Renames images to their md5sum or md5sum-copy-#.
  3. # Usage:    sh image-renamer <directory>
  4. #   Example:  sh image-renamer .
  5. #   Example:  sh image-renamer "main directory"/sub\ directory/subdir
  6.  
  7. # -maxdepth 1:   Don't search deeper into directories
  8. # -type f:       Only find regular files (not directories, links, etc.)
  9. # -iname "...":  Case insensitive search on filename
  10. find "$1" -maxdepth 1 -type f -iname "*[.png|.jpg|.jpeg|.gif|.bmp]" -print | while read FILE
  11. do
  12.   DIR=`dirname "$FILE"`                            # File directory
  13.   EXT=`echo "$FILE" | rev | cut -d '.' -f 1 | rev` # File extension/type
  14.   NAME=`basename "$FILE" ".$EXT"`                  # File name, not used
  15.   # echo "["$DIR"] ["$NAME"] ["$EXT"]"
  16.  
  17.   MD5=`md5sum "$FILE" | cut -d ' ' -f 1`
  18.   # echo "["$MD5"]"
  19.  
  20.   NEW_FILE=""$DIR"/"$MD5"."$EXT""
  21.  
  22.   # If NEW_FILE exists...
  23.   if [ -f "$NEW_FILE" ]; then
  24.     for i in $(seq 1 50); do
  25.       NEW_FILE=""$DIR"/"$MD5"-copy-$i."$EXT""
  26.       # If NEW_FILE does not exist...
  27.       if [ ! -f "$NEW_FILE" ]; then
  28.         # Figured out a valid file name; exit loop
  29.         break
  30.       fi
  31.     done
  32.   fi
  33.  
  34.   # echo "["$NEW_FILE"]"
  35.  
  36.   # Use move command to rename so can do "-n"
  37.   #   -n:  Do not overwrite an existing file
  38.   #   -v:  Verbose; explain everything being done
  39.   mv -n -v "$FILE" "$NEW_FILE"
  40. done
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement