Created
April 4, 2022 16:47
-
-
Save MatMercer/40532aefca260704cb227de412479a71 to your computer and use it in GitHub Desktop.
Archive files by year and month from the current folder in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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