4/20/2020

Sorting files by folders in linux


#!/bin/bash

DIR=~/Documents/photos/main/unsorted

target=$DIR
cd "$DIR"

for file in *.JPG
do
    echo "processing file $file"
    year=$(stat $file | grep -oP "(?<=(Modify:\s)).*?\d{4}")
    month=$(stat $file | stat $file |  grep -oP "(?<=(Modify:\s)).*?\d{4}-\d{2}")

    if [ ! -d "$target/$year" ]; then
        mkdir "$target/$year"
        echo "starting new year: $year"
    fi
    if [ ! -d "$target/$year/$month" ]; then
        mkdir "$target/$year/$month"
        echo "starting month folder: $month"
    fi
    echo "moving file $file"
    mv "$file" "$target/$year/$month"

done

No comments:

Post a Comment