BASH – Picture / Video File Name Manipulation

| | | | | | | | | | | | | |

Often there are needs to rename a picture or video to help with organizing the files on a computer or in cloud storage. The code below can help to make some organization easier.

For those that are not use to the date format used below, it is specifically chosen to make it easier to organize first by year then month then day. If someone wanted they could go down further in organizing but for my needs year, month, and day are usually good enough.

Bash
#!/bin/sh

echo "Please type in the photographer name or simply press enter/return. "
read photographer

#region JPG or DNG
if compgen -G "*.jpg" >/dev/null || compgen -G "*.dng" >/dev/null; then
    # File is a picture that should have exif data

    # This first part will rename a picture based on the date and time the picture was taken.
    # NOTE: This will only work properly if you have the original files. It usually will not work if you downloaded the pictures from social media or other sources.
    exiftool '-filename<${datetimeoriginal#;DateFmt("%Y-%m-%d_%H.%M.%S")}${subsectimeoriginal}0.%e' .

    # This part will add the image size to the file name
    exiftool '-filename<%f_$imagesize.%e' .

    find . -name \*.jpg -o -name \*.dng >picture_files.txt
    while IFS="" read -r picture || [ -n "$picture" ]; do

        # Try to create a rename directory
        mkdir -p rename/pictures

        filename=$(basename -- "$picture")
        extension="${filename##*.}"
        filename="${filename%.*}"

        if [ ! -z $photographer ]; then
            # If photographer is defined then add it to the file name
            cp -v ${picture} "rename/pictures/${filename}_${photographer}.${extension}"
            # mv -v ${picture} "rename/pictures/${filename}_${photographer}.${extension}" # Once you are comfortable with the copy results you can comment/remove the copy and uncomment the move
        else
            cp -v ${picture} "rename/pictures/${filename}.${extension}"
            # mv -v ${picture} "rename/pictures/${filename}.${extension}" # Once you are comfortable with the copy results you can comment/remove the copy and uncomment the move
        fi

    done <picture_files.txt
    rm -fv picture_files.txt
fi
#endregion JPG or DNG

# region AVI, MP4, MOV
if [ ! -z $photographer ]; then
    if compgen -G "*.avi" >/dev/null || compgen -G "*.mp4" >/dev/null || compgen -G "*.mov" >/dev/null; then

        find . -name \*.avi -o -name \*.mp4 -o -name \*.mov >video_files.txt
        while IFS="" read -r video || [ -n "$video" ]; do

            # Try to create a rename directory
            mkdir -p rename/videos

            filename=$(basename -- "${video}")
            extension="${filename##*.}"
            filename="${filename%.*}"

            newname="${filename}_${photographer}"

            echo "Original Name: ${video}"
            echo "New Name: $newname.${extension}"

            cp -v "${video}" "rename/videos/${newname}.${extension}"
            # mv -v "${video}" "rename/videos/${newname}.${extension}" # Once you are comfortable with the copy results you can comment/remove the copy and uncomment the move

        done <video_files.txt
        rm -fv video_files.txt
    fi
fi
# endregion AVI, MP4, MOV
Originally Posted on August 19, 2023
Last Updated on October 15, 2025
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.