Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Script moulded by freddy87
- #Version 2
- read -p "Enter the filetype you want to rename: ." extension #option to select filename extension
- read -p "Enter a prefix for the renamed files: " prefix #option to put a prefix eg. Birthday_
- file_num=$(ls -1 *.$extension| grep -v '/$' | wc -l) #counts files in folder of type $extension
- echo "Number of files to rename: "$file_num #output for peace of mind
- padding=${#file_num} #gets length of file_num for padding to give each filename
- echo "Length of padding in filename: "$padding #output for peace of mind
- if [ "$extension" = "JPG" ]; then
- for f in *.$extension; do
- mv "$f" "${f%.JPG}.jpg"
- done
- echo "*.JPG renamed to *.jpg"
- a=1
- for i in *.$extension; do
- if [ -n "$prefix" ]; then
- new=$(printf $prefix"_%0"$padding"d."$extension "$a") #$padding appropriate to number of files
- if [ "$prefix""_""$new" = "$i" ]; then
- echo "Error: Filenames matching already"
- elif [ "$new" != "$i" ]; then
- mv -- "$i" "$new" #renames files
- let a=a+1
- fi
- elif [ -z "$prefix" ]; then
- new=$(printf "%0"$padding"d."$extension "$a") #$padding appropriate to number of files
- if [ "$new" = "$i" ]; then
- echo "Error: Filenames matching already"
- elif [ "$new" != "$i" ]; then
- mv -- "$i" "$new" #renames files
- let a=a+1
- fi
- fi
- done
- elif [ "$extension" != "JPG" ]; then
- a=1
- for i in *.$extension; do
- if [ -n "$prefix" ]; then
- new=$(printf $prefix"_%0"$padding"d."$extension "$a") #$padding appropriate to number of files
- if [ "$prefix""_""$new" = "$i" ]; then
- echo "Error: Filenames matching already"
- elif [ "$new" != "$i" ]; then
- mv -- "$i" "$new" #renames files
- let a=a+1
- fi
- elif [ -z "$prefix" ]; then
- new=$(printf "%0"$padding"d."$extension "$a") #$padding appropriate to number of files
- if [ "$new" = "$i" ]; then
- echo "Error: Filenames matching already"
- elif [ "$new" != "$i" ]; then
- mv -- "$i" "$new" #renames files
- let a=a+1
- fi
- fi
- done
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement