Mac Desktop Screenshot Cleanup Script
January 18, 2020
I take a lot of screenshots on my Mac. Each screenshot saves to the desktop by default and has a filename like
Screen Shot 2020-01-18 at 4.38.20 PM. Every few weeks I manually drag all my screenshots to the trash. As I started learning more about shell scripts this seemed like the perfect task to automate. I rename a screenshot file whenever I want to keep it, making any screenshot with a default name one I’ll later delete. This script deletes all Mac Screen Shot files with default names from the desktop.
#!/bin/bash # Script deletes all screenshots on mac desktop that haven't been renamed # Default name is Screen Shot * FILE_COUNT=0 DIR="/Users/$USER/Desktop" FILE_START_TEXT="Screen Shot" FILE_TO_DELETE="$DIR/$FILE_START_TEXT" for FILE in "$FILE_TO_DELETE"* do # $FILE is the full path and file name if [ "$FILE" = "$FILE_TO_DELETE*" ] then echo "No Screen Shot files to delete in $DIR" # if no files exit program after this message exit 0 else # increment FILE_COUNT ((FILE_COUNT=FILE_COUNT+1)) # delete the file rm -f "$FILE" fi done # echo how many files were deleted if [ "$FILE_COUNT" -eq 1 ] then echo "$FILE_COUNT screen shot deleted from $DIR" else echo "$FILE_COUNT screen shots deleted from $DIR" fi exit 0
I saved a file called
dsc.sh to the home directory. To run the script, first give it the proper permissions with
chmod 755 dsc.sh. Then run
There are three possible messages it will return.
- No screenshots on the desktop to delete:
No Screen Shot files to delete in /Users/Jordan/Desktop
- One screenshot to delete:
1 screen shot deleted from /Users/Jordan/Desktop
- Multiple screenshots to delete:
23 screen shots deleted from /Users/Jordan/Desktop