Skip to content

Instantly share code, notes, and snippets.

@MatMercer
Created April 4, 2022 16:47
Show Gist options
  • Save MatMercer/40532aefca260704cb227de412479a71 to your computer and use it in GitHub Desktop.
Save MatMercer/40532aefca260704cb227de412479a71 to your computer and use it in GitHub Desktop.
Archive files by year and month from the current folder in bash
#!/bin/bash
currentYearMonth=$(date "+%Y-%m")
echo "Ignoring files from $currentYearMonth"
find . -maxdepth 1 -not -name "archive.sh" -print0 | while read -d $'\0' file
do
excludeDate=0
[[ "$file" =~ [0-9]{4}-[0-9]{2} ]] && excludeDate=1
#fileTargetFolder=$(stat -c "%Z" "$file" | xargs -I {} date -d @{} "+%Y-%m")
fileTargetFolder=$(date -r "$file" "+%Y-%m")
if [ "$excludeDate" != 1 ] && [ "$currentYearMonth" != "$fileTargetFolder" ]; then
echo $file
mkdir -p "$fileTargetFolder"
mv -v "$file" "$fileTargetFolder"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment