Bash – Rename Picture

| | | |

for f in *.HEIC; do
    mv -v "$f" "${f%.HEIC}.heic"
done

# 1
set -eu -o pipefail


# 2
count=$(find . -depth 1 -name "*.heic" | wc -l | sed 's/[[:space:]]*//')
echo "converting $count files .heic files to .jpg"

# 3
magick mogrify -monitor -format jpg *.heic

# 4
echo "Remove .heic files? [y/n]"
read remove

# 5
if [[ "$remove" == "y" ]]; then
  find . -depth 1 -name "*.heic" -delete
fi


for f in *.JPG; do
    mv -v "$f" "${f%.JPG}.jpg"
done

for f in *.AVI; do
    mv -v "$f" "${f%.AVI}.avi"
done

for f in *.MP4; do
    mv -v "$f" "${f%.MP4}.mp4"
done

for f in *.MOV; do
    mv -v "$f" "${f%.MOV}.mov"
done

for f in *.HEIC; do
    mv -v "$f" "${f%.HEIC}.heic"
done

set -eu -o pipefail
count=$(find . -depth 1 -name "*.heic" | wc -l | sed 's/[[:space:]]*//')
echo "converting $count files .heic files to .jpg"
magick mogrify -monitor -format jpg *.heic
find . -depth 1 -name "*.heic" -delete

# Add 12 hours to the picture's EXIF info
# exiftool "-AllDates+=0:0:0 12:0:0" -verbose *.jpg

# Subtract 12 hours to the picture's EXIF info
# exiftool "-AllDates-=0:0:0 12:0:0" -verbose *.jpg

photographer=""
# photographer="David"

mkdir -p rename

count=0
for video in *.mp4; do
    let "count+=1"
    # echo $video

    originalname="${video%.*}"
    newname="${video%.*}_${photographer}"

    echo "Original Name: $video"
    echo "New Name: $newname.mp4"

    if [ ! -z $photographer ]; then
        mv "$video" "rename/${newname}.mp4"
    fi
done

count=0
for video in *.avi; do
    let "count+=1"
    # echo $video

    originalname="${video%.*}"
    newname="${video%.*}_${photographer}"

    echo "Original Name: $video"
    echo "New Name: $newname.avi"

    if [ ! -z $photographer ]; then
        mv "$video" "rename/${newname}.avi"
    fi
done

count=0
for video in *.mov; do
    let "count+=1"
    # echo $video

    originalname="${video%.*}"
    newname="${video%.*}_${photographer}"

    echo "Original Name: $video"
    echo "New Name: $newname.mov"

    if [ ! -z $photographer ]; then
        mv "$video" "rename/${newname}_$(du -h "$video" | awk '{ print $1}').mov"
    fi
done

count=0
mkdir -p rename
for picture in *.jpg; do
    let "count+=1"
    echo $picture

    originalname="${picture%.*}"
    xpath=${picture%/*}
    pxwidth=$(exif $picture | grep "Pixel X Dimension" | cut -d "|" -f2)
    pxheight=$(exif $picture | grep "Pixel Y Dimension" | cut -d "|" -f2)
    dateTaken=$(exif $picture | grep "Date and Time (Digit" | cut -d "|" -f2 | cut -d " " -f1)

    if test -z "$dateTaken"; then
        echo "Date/Time not found for $picture"
    else
        datetaken="${dateTaken//:/-}"

        timeTaken=$(exif $picture | grep "Date and Time (Digit" | cut -d "|" -f2 | cut -d " " -f2)
        timetaken="${timeTaken//:/.}"

        if test -z "$pxwidth" || test -z "$pxheight"; then
            newname="$(echo $datetaken)_$(echo $timetaken)_$(du -h "$video" | awk '{ print $1}')"
        else
            newname="$(echo $datetaken)_$(echo $timetaken)_$(echo $pxwidth)x$(echo $pxheight)_$(du -h "$video" | awk '{ print $1}')"
        fi

        echo "Original Name: $picture"
        echo "New Name: $newname.jpg"

        if [[ -e $newname.jpg || -L $newname.jpg ]]; then
            i=1
            while [[ -e $newname-$i.jpg || -L $newname-$i.jpg ]]; do
                let i++
            done
            newname=$newname-$i
            echo "New Numbered Name: $newname"
            echo " "
        fi

        if [ ! -z $photographer ]; then
            mv "$picture" "rename/${newname}_${photographer}.jpg"
        else
            mv "$picture" "rename/${newname}.jpg"
        fi
    fi
done

Originally Posted on November 15, 2022
Last Updated on August 17, 2024
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.