Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

BASH – Picture / Video File Name Manipulation

Posted on August 19, 2023August 17, 2024 By David Kittell

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.

#!/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 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.

Related

CentOS Code Debian Fedora Mac OS X Shell Mac OSX Raspberry Pi Raspbian (Raspberry Pi OS) Red Hat Ubuntu UNIX UNIX Shell Scripts Zorin OS

Post navigation

Previous post
Next post

Related Posts

Mac OSX Terminal – Create SSH Key

Posted on December 20, 2022August 17, 2024

cd ~/.ssh ssh-keygen -t ed25519 -C "<email address>" -f BitBucket cat BitBucket.pub Originally Posted on December 20, 2022Last 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…

Read More

Format US Phone

Posted on August 6, 2013October 26, 2015

Worst case scenario, for when you do not have the ability to do Regex replace. SELECT dbo.FormatUSPhone(‘989.989.9898’) AS Phone ,dbo.FormatUSPhone(‘(989)989.9898’) AS Phone2 ,dbo.FormatUSPhone(‘(989) 989-9898’) AS Phone3 ,dbo.FormatUSPhone(‘(989) 989-989’) AS Phone4 ,dbo.FormatUSPhone(‘989/989/9898’) AS Phone5 ,dbo.FormatUSPhone(‘9899899898’) AS Phone6 Phone Phone2 Phone3 Phone4 Phone5 Phone6 ————————- ————————- ————————- ————————- ————————- ————————- (989) 989-9898…

Read More

Find Most Expensive Queries Using DMV

Posted on April 29, 2013October 26, 2015

SELECT TOP 10 SUBSTRING(qt.TEXT, (qs.statement_start_offset / 2) + 1, ( ( CASE qs.statement_end_offset WHEN – 1 THEN DATALENGTH(qt.TEXT) ELSE qs.statement_end_offset END – qs.statement_start_offset ) / 2 ) + 1) ,qs.execution_count ,qs.total_logical_reads ,qs.last_logical_reads ,qs.total_logical_writes ,qs.last_logical_writes ,qs.total_worker_time ,qs.last_worker_time ,qs.total_elapsed_time / 1000000 total_elapsed_time_in_S ,qs.last_elapsed_time / 1000000 last_elapsed_time_in_S ,qs.last_execution_time ,qp.query_plan FROM sys.dm_exec_query_stats qs CROSS…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes