Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Renames images to their md5sum or md5sum-copy-#.
- # Usage: sh image-renamer <directory>
- # Example: sh image-renamer .
- # Example: sh image-renamer "main directory"/sub\ directory/subdir
- # -maxdepth 1: Don't search deeper into directories
- # -type f: Only find regular files (not directories, links, etc.)
- # -iname "...": Case insensitive search on filename
- find "$1" -maxdepth 1 -type f -iname "*[.png|.jpg|.jpeg|.gif|.bmp]" -print | while read FILE
- do
- DIR=`dirname "$FILE"` # File directory
- EXT=`echo "$FILE" | rev | cut -d '.' -f 1 | rev` # File extension/type
- NAME=`basename "$FILE" ".$EXT"` # File name, not used
- # echo "["$DIR"] ["$NAME"] ["$EXT"]"
- MD5=`md5sum "$FILE" | cut -d ' ' -f 1`
- # echo "["$MD5"]"
- NEW_FILE=""$DIR"/"$MD5"."$EXT""
- # If NEW_FILE exists...
- if [ -f "$NEW_FILE" ]; then
- for i in $(seq 1 50); do
- NEW_FILE=""$DIR"/"$MD5"-copy-$i."$EXT""
- # If NEW_FILE does not exist...
- if [ ! -f "$NEW_FILE" ]; then
- # Figured out a valid file name; exit loop
- break
- fi
- done
- fi
- # echo "["$NEW_FILE"]"
- # Use move command to rename so can do "-n"
- # -n: Do not overwrite an existing file
- # -v: Verbose; explain everything being done
- mv -n -v "$FILE" "$NEW_FILE"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement