Daily screenshot management w/ Crontab on MacOS

Siddhant Khare - Sep 6 - - Dev Community

In this blog post, we'll explore how to efficiently manage your screenshots on MacOS using crontab and Google Drive file stream. This solution is perfect for those struggling with limited storage on their Mac and want to keep their screenshot folder tidy while maintaining easy access to their captures.

Environment

  • MacOS with zsh shell
  • Google Drive file stream (unlimited storage - well 15 GB only :))

Objectives

  1. Address MacOS storage limitations
  2. Preserve screenshots and screen recordings
  3. Prevent daily accumulation of files in the screenshot folder
  4. Maintain easy access to screenshots from the Dock

Screenshot Configuration

First, set your screenshot save location to a specific folder within the Google Drive file stream. This ensures that your screenshots are automatically backed up to the cloud.

Shell Script

We'll create a shell script that performs the following tasks:

  1. Check if there are files in the screenshots folder
  2. If files exist:
    • Copy all files to a dated folder in the archive location
    • Remove all files from the screenshots folder
  3. If no files exist, echo a message

Here's the shell script (~/scripts/mv_screenshots.sh):

#!/bin/zsh

target=/Volumes/GoogleDrive/My\ Drive/Images/screenshots
archive=/Volumes/GoogleDrive/My\ Drive/Images/prev-screenshots

if find "$target" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
    setopt localoptions rmstarsilent
    cp -R "$target" "$archive/`date +%Y-%m-%d`"
    rm "$target"/*
else
    echo "Target '$target' is empty"
fi
Enter fullscreen mode Exit fullscreen mode

Let's break down the script:

  • We define the target (screenshots) and archive (prev-screenshots) directories.
  • The find command checks if there are any files in the target directory.
  • If files exist, we use setopt localoptions rmstarsilent to suppress confirmation prompts when removing files.
  • We copy the entire target directory to the archive, creating a new folder with today's date.
  • Finally, we remove all files from the target directory.
  • If no files are found, we simply echo a message.

Crontab Configuration

To automate this process, we'll use crontab to run the script daily at 11:30 PM and log the results. Here's how to set it up:

  1. Open your terminal and type crontab -e to edit your crontab file.
  2. Add the following line:
30 23 * * * zsh ~/scripts/mv_screenshots.sh >> ~/log/job_`date +\%Y-\%m-\%d`.log 2>&1
Enter fullscreen mode Exit fullscreen mode

This crontab entry does the following:

  • Runs at 11:30 PM every day (30 23 * * *)
  • Executes the script using zsh
  • Appends the output to a log file with today's date
  • Redirects both stdout and stderr to the log file (2>&1)

Conclusion

With this setup, your screenshots will be automatically organized and archived daily. Your main screenshots folder will remain uncluttered, while you maintain easy access to your recent captures. The dated archive folders in Google Drive ensure that you can always find older screenshots when needed.

Remember to adjust the paths in the shell script to match your Google Drive File Stream setup. Happy screenshot managing!


For more tips and insights on security and log analysis, follow me on Twitter @Siddhant_K_code and stay updated with the latest & detailed tech content like this.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player