Skip to content

Instantly share code, notes, and snippets.

@arvati
Last active April 28, 2024 19:55
Show Gist options
  • Save arvati/41e491ded08acc7decdbf160392cb527 to your computer and use it in GitHub Desktop.
Save arvati/41e491ded08acc7decdbf160392cb527 to your computer and use it in GitHub Desktop.
Move files with date in file name to directories
#!/bin/bash

declare -a months=("01 - Janeiro" "02 - Fevereiro" "03 - Março" "04 - Abril" "05 - Maio" "06 - Junho" "07 - Julho" "08 - Agosto" "09 - Setembro" "10 - Outubro" "11 - Novembro" "12 - Dezembro")

for ano in /mnt/data/media/Pictures/Dropbox/video/*; do
    if [  -d "${ano}" ] && [ $((10#$(basename "${ano}"))) -le 2024 ]; then
        for folder in "${ano}"/*; do
            pasta=$(basename "${folder}")
            nro=$((10#${pasta}))
            if [  -d "${folder}" ] && [ ${nro} -le 12 ] && [ ${#pasta} -le 2 ]; then
                dst=$(dirname "${folder}")/"${months[${nro}-1]}"
                echo "${folder} into ${dst}"
                mv "${folder}" "${dst}"
            fi
        done
    fi
done
ls /mnt/data/media/Pictures/Dropbox
'2014-09-28 00.21.48.jpg'     '2015-05-09 16.02.28.jpg'    '2016-01-07 15.48.13.jpg'
'2014-09-28 19.23.47.jpg'     '2015-05-09 16.02.35.jpg'    '2016-01-07 15.48.18.jpg'
#!/bin/bash

cd /mnt/data/media/Pictures/Dropbox
for file in *.jpg; do
  date=$(printf "$file" | sed -nE 's/.*^([0-9]{4})-([01][0-9]).*/\1\/\2/p')
  if [ -n "$date" ]; then
    mkdir -p "$date" && mv "$file" "$date"
  fi
done
/mnt/data/media/Pictures/Dropbox/2014/09/2014-09-28 00.21.48.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment