Guest User

MonthFolders revision 2025-10-03

a guest
Oct 3rd, 2025
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.47 KB | Source Code | 0 0
  1. # MonthFolders: organizes files by monthly directories. CC0 1.0 public domain.
  2.  
  3. filecount=$(find -maxdepth 1 -type f |wc -l)
  4. if [ $filecount -eq 0 ]; then
  5.     echo This directory contains no files.
  6.     return 1; # close script because nothing to do.
  7. fi
  8.  
  9. startyear=$(find -maxdepth 1 -type f -printf '%TY\n' |sort |head -n 1)
  10. endyear=$(find -maxdepth 1 -type f -printf '%TY\n' |sort |tail -n 1)
  11. yearcount=0 # initialize variable
  12. yearcount=$startyear
  13.  
  14.  
  15. if [ $filecount -eq 1 ]; then
  16.     echo "This directory contains one file from the year $startyear."
  17. elif [ $startyear -eq $endyear ]; then
  18.     echo "This directory contains $filecount files from the year $startyear."
  19. else
  20.     echo "This directory contains $filecount files between the years $startyear and $endyear."
  21. fi
  22.    
  23. while [ $yearcount -le $endyear ]; do
  24.     # skip years with no files
  25.     while [ $(find -maxdepth 1 -type f -newermt $yearcount-01-01 -not -newermt $((yearcount+1))-01-01 |wc -l) -eq 0 ] && [ $yearcount -lt $endyear ]; do
  26.         yearcount=$(($yearcount+1));
  27.     done
  28.    
  29.     printf "Organizing files from $yearcount..." # later completed with "Done."
  30.     month_processed=1 # reset to January
  31.     while [ $month_processed -le 11 ]; do
  32.     # pad 0-9 with zero.
  33.         monthcount=$month_processed
  34.         nextmonth=$(($month_processed+1));
  35.         if [ $month_processed -eq 9 ]; then monthcount=09; fi
  36.         if [ $month_processed -lt 9 ]; then
  37.             monthcount=$(printf 0$monthcount);
  38.             nextmonth=$(printf 0$nextmonth);
  39.         fi
  40.         count_files_in_month=$(find -maxdepth 1 -type f -newermt $yearcount-$monthcount-01 -not -newermt $yearcount-$nextmonth-01 |wc -l)
  41.         # Only create directory if files from that month actually exist.
  42.         if [ $count_files_in_month -gt 0 ]; then
  43.             printf " $monthcount"
  44.             if [ ! -d "$yearcount-$monthcount" ]; then mkdir "$yearcount-$monthcount"; fi
  45.             find -maxdepth 1 -type f -newermt $yearcount-$monthcount-01 -not -newermt $yearcount-$nextmonth-01 -exec mv -n "{}" "$yearcount-$monthcount" \;;
  46.         fi
  47.         month_processed=$(($month_processed+1));
  48.     done
  49.     # Separate code for December because there is no thirteenth month.
  50.     count_files_in_month=$(find -maxdepth 1 -type f -newermt $yearcount-12-01 -not -newermt $(($yearcount+1))-01-01 |wc -l)
  51.     if [ $count_files_in_month -gt 0 ]; then
  52.         printf " 12"
  53.         if [ ! -d "$yearcount-12" ]; then mkdir "$yearcount-12"; fi
  54.         find -maxdepth 1 -type f -newermt $yearcount-12-01 -not -newermt $(($yearcount+1))-01-01 -exec mv -n "{}" "$yearcount-12" \;;
  55.     fi
  56.    
  57.     printf " Done.\n"
  58.     yearcount=$(($yearcount+1));
  59. done
Advertisement
Add Comment
Please, Sign In to add comment