Idea

I have a folder containing a bunch of photos and videos. Some of these files do not have a timestamp indicating when they were taken.

Task

  • check all files for the datetime stamp
  • move video files to separate folder video_dir
  • move files without timestamp to folder missing_date_dir.

What to do

  1. create bash script

nano your-file-name-here.sh

  1. copy the code

#!/bin/bash

# Ask the user for the target directory
echo "Enter the target directory containing the image and video files:"
read target_dir

# Check if the directory exists
if [ ! -d "$target_dir" ]; then
    echo "The directory does not exist. Exiting."
    exit 1
fi

# Change to the target directory
cd "$target_dir" || exit

# Create folders for each file type
mkdir -p HEIC_files CR2_files RAF_files DNG_files video

# Function to convert files and preserve timestamps
convert_and_preserve() {
    local extension=$1
    local folder=$2

    # Match files case-insensitively
    shopt -s nocaseglob
    if compgen -G "*.$extension" > /dev/null; then
        for file in *.$extension; do
            # Extract the original timestamps
            original_time=$(stat -c %y "$file")
            
            # Create a temporary JPG file
            filename="${file%.*}"
            magick "$file" "$filename.jpg"  # Use `convert` instead of `mogrify` to avoid overwriting
            
            # Restore the original timestamp
            touch -d "$original_time" "$filename.jpg"
        done
        
        # Move original files to the designated folder
        mv *.$extension "$folder/"
        echo "$extension files converted and moved."
    else
        echo "No $extension files found."
    fi
    shopt -u nocaseglob
}

# Convert HEIC files to JPEG
convert_and_preserve "HEIC" "HEIC_files"

# Convert CR2 files to JPEG
convert_and_preserve "CR2" "CR2_files"

# Convert RAF files to JPEG
convert_and_preserve "RAF" "RAF_files"

# Convert DNG files to JPEG
convert_and_preserve "DNG" "DNG_files"

# Organize JPEG files by creation date
shopt -s nocaseglob  # Enable case-insensitive globbing
if compgen -G "*.jpg" > /dev/null; then
    for jpg_file in *.jpg; do
        # Extract the creation year and month
        creation_date=$(stat -c %y "$jpg_file" | cut -d'-' -f1,2 | tr -d '-')
        
        # Create the YYYYMM folder if it doesn't exist
        mkdir -p "$creation_date"
        
        # Move the JPG file to the corresponding folder
        mv "$jpg_file" "$creation_date/"
        echo "Moved $jpg_file to folder $creation_date"
    done
else
    echo "No JPG files found to organize."
fi
shopt -u nocaseglob  # Disable case-insensitive globbing

# Find and move all video files based on MIME type
find . -type f -exec file --mime-type {} + | grep "video/" | cut -d: -f1 | while read -r video_file; do
    mv "$video_file" video/
    echo "Moved video file: $video_file"
done

# Completion message
echo "Conversion, organization, and video file handling complete."
  1. save the file
  2. change permission chmod +x your-filename-here.sh
  3. execute ./find-filename-here.sh
  4. after the script execution you will get the message Enter the directory containing the media files:
  5. paste relative path to folder and press enter.