Skip to content

Instantly share code, notes, and snippets.

@aaronshaf
Created June 2, 2023 17:37
Show Gist options
  • Save aaronshaf/de35e048b9b7e46b975a5ed397e3ac7a to your computer and use it in GitHub Desktop.
Save aaronshaf/de35e048b9b7e46b975a5ed397e3ac7a to your computer and use it in GitHub Desktop.
sift files into folders by year (or year and month)
#!/bin/bash
## this script moves all files (not folders) in ~/Documents into folder called ~/Documents/[year]
## where [year] is the year the file was created
# Find files with extensions and move them into appropriate year folders
find ~/Documents -maxdepth 1 ! -name ".*" -name "*.*" | while IFS= read -r file; do
# Extract the year from the file's creation timestamp
year=$(stat -f "%Sm" -t "%Y" "$file")
# Create the year folder if it doesn't exist
mkdir -p "$HOME/Documents/$year"
# Move the file into the year folder
mv "$file" "$HOME/Documents/$year/"
echo "Moved file: $file to $HOME/Documents/$year/"
done
#!/bin/bash
# Find files with extensions and move them into appropriate year-month folders
find ~/Downloads -maxdepth 1 ! -name ".*" -name "*.*" | while IFS= read -r file; do
# Extract the year and month from the file's modification timestamp
year=$(stat -f "%Sm" -t "%Y" "$file")
month=$(stat -f "%Sm" -t "%m" "$file")
# Create the year-month folder if it doesn't exist
mkdir -p "$HOME/Downloads/$year-$month"
# Move the file into the year-month folder
mv "$file" "$HOME/Downloads/$year-$month/"
echo "Moved file: $file to $HOME/Downloads/$year-$month/"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment