Skip to content

Instantly share code, notes, and snippets.

@azer
Created February 25, 2025 10:03
Show Gist options
  • Save azer/b1b0c16e9b70caa7a35d62bc506e4105 to your computer and use it in GitHub Desktop.
Save azer/b1b0c16e9b70caa7a35d62bc506e4105 to your computer and use it in GitHub Desktop.
Desktop tidier
#!/bin/bash
# Desktop Tidier
# A simple script to organize files on your macOS desktop
# Colors for prettier output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${BLUE}==== Desktop Tidier ====${NC}"
echo "Organizing your desktop files..."
# Navigate to the Desktop
cd ~/Desktop || { echo -e "${RED}Couldn't access Desktop folder!${NC}"; exit 1; }
# Create main organizing folder if it doesn't exist
MAIN_FOLDER="tidy"
mkdir -p "$MAIN_FOLDER"
# Define all possible category folders
SCREENSHOTS_FOLDER="$MAIN_FOLDER/Screenshots"
SCREENCASTS_FOLDER="$MAIN_FOLDER/Screencasts"
DOCS_FOLDER="$MAIN_FOLDER/Docs"
PICS_FOLDER="$MAIN_FOLDER/Images"
VIDEOS_FOLDER="$MAIN_FOLDER/Videos"
AUDIO_FOLDER="$MAIN_FOLDER/Audio"
CODE_FOLDER="$MAIN_FOLDER/Code"
ARCHIVES_FOLDER="$MAIN_FOLDER/Archives"
TORRENTS_FOLDER="$MAIN_FOLDER/Torrents"
APPS_FOLDER="$MAIN_FOLDER/Apps"
OTHERS_FOLDER="$MAIN_FOLDER/Other"
# Create all folders upfront to avoid issues
mkdir -p "$SCREENSHOTS_FOLDER" "$SCREENCASTS_FOLDER" "$DOCS_FOLDER" "$PICS_FOLDER" "$VIDEOS_FOLDER" \
"$AUDIO_FOLDER" "$CODE_FOLDER" "$ARCHIVES_FOLDER" "$TORRENTS_FOLDER" "$APPS_FOLDER" "$OTHERS_FOLDER"
# Counters for summary
screenshots_count=0
screencasts_count=0
docs_count=0
pics_count=0
videos_count=0
audio_count=0
code_count=0
archives_count=0
torrents_count=0
apps_count=0
others_count=0
errors_count=0
# Process files using while read loop for better control
find . -maxdepth 1 -type f -not -name ".*" -not -name "$(basename "$0")" | while IFS= read -r filepath; do
# Extract just the filename without the leading ./
filename="${filepath#./}"
# Skip the main folder and hidden files
if [[ "$filename" == "$MAIN_FOLDER"* ]] || [[ "$filename" == .* ]]; then
continue
fi
# For files that start with dash, we need to use -- to signal the end of options
if [[ "$filename" == -* ]]; then
mv_prefix="mv -- "
else
mv_prefix="mv "
fi
# Screenshots (macOS naming pattern)
if [[ "$filename" == Screenshot* ]]; then
if eval "$mv_prefix\"$filename\" \"$SCREENSHOTS_FOLDER/\""; then
((screenshots_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
continue
fi
# Screen recordings
if [[ "$filename" == *"Screen Recording"* ]] || [[ "$filename" == *"screen recording"* ]]; then
if eval "$mv_prefix\"$filename\" \"$SCREENCASTS_FOLDER/\""; then
((screencasts_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
continue
fi
# Get file extension (safely handling files with no extension)
if [[ "$filename" == *.* ]]; then
extension="${filename##*.}"
extension=$(echo "$extension" | tr '[:upper:]' '[:lower:]')
else
extension=""
fi
# File extension-based sorting
case "$extension" in
# Documents
pdf|docx|doc|txt|rtf|pages|xlsx|xls|csv|pptx|ppt|key|md|rtfd|numbers)
if eval "$mv_prefix\"$filename\" \"$DOCS_FOLDER/\""; then
((docs_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Images
jpg|jpeg|png|gif|tiff|tif|webp|heic|svg|psd|ai|bmp|raw|cr2|nef)
if eval "$mv_prefix\"$filename\" \"$PICS_FOLDER/\""; then
((pics_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Audio files
mp3|wav|aac|ogg|m4a|flac|alac|aiff|wma)
if eval "$mv_prefix\"$filename\" \"$AUDIO_FOLDER/\""; then
((audio_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Videos
mp4|mov|avi|mkv|wmv|flv|webm|m4v)
if eval "$mv_prefix\"$filename\" \"$VIDEOS_FOLDER/\""; then
((videos_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Code files - exclude the tidier script itself
py|js|ts|html|css|cpp|c|h|java|rb|php|go|rs|swift|pl|json|xml|yaml|yml)
if eval "$mv_prefix\"$filename\" \"$CODE_FOLDER/\""; then
((code_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Exclude the tidier script itself but include other shell scripts
sh)
if [[ "$filename" != "$(basename "$0")" ]]; then
if eval "$mv_prefix\"$filename\" \"$CODE_FOLDER/\""; then
((code_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
fi
;;
# SQL files (could be docs or code, we're putting them in code)
sql)
if eval "$mv_prefix\"$filename\" \"$CODE_FOLDER/\""; then
((code_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Archives
zip|rar|7z|gz|tar|bz2|xz|iso)
if eval "$mv_prefix\"$filename\" \"$ARCHIVES_FOLDER/\""; then
((archives_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Torrents
torrent)
if eval "$mv_prefix\"$filename\" \"$TORRENTS_FOLDER/\""; then
((torrents_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Apps and installers
app|pkg|exe|msi|dmg)
if eval "$mv_prefix\"$filename\" \"$APPS_FOLDER/\""; then
((apps_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
# Everything else
*)
if eval "$mv_prefix\"$filename\" \"$OTHERS_FOLDER/\""; then
((others_count++))
else
echo -e "${RED}Failed to move: $filename${NC}"
((errors_count++))
fi
;;
esac
done
# Get final counts from temp files
screenshots_count=$(find "$SCREENSHOTS_FOLDER" -type f | wc -l)
screencasts_count=$(find "$SCREENCASTS_FOLDER" -type f | wc -l)
docs_count=$(find "$DOCS_FOLDER" -type f | wc -l)
pics_count=$(find "$PICS_FOLDER" -type f | wc -l)
videos_count=$(find "$VIDEOS_FOLDER" -type f | wc -l)
audio_count=$(find "$AUDIO_FOLDER" -type f | wc -l)
code_count=$(find "$CODE_FOLDER" -type f | wc -l)
archives_count=$(find "$ARCHIVES_FOLDER" -type f | wc -l)
torrents_count=$(find "$TORRENTS_FOLDER" -type f | wc -l)
apps_count=$(find "$APPS_FOLDER" -type f | wc -l)
others_count=$(find "$OTHERS_FOLDER" -type f | wc -l)
# Clean up empty folders
if [ "$screenshots_count" -eq 0 ]; then rmdir "$SCREENSHOTS_FOLDER" 2>/dev/null; fi
if [ "$screencasts_count" -eq 0 ]; then rmdir "$SCREENCASTS_FOLDER" 2>/dev/null; fi
if [ "$docs_count" -eq 0 ]; then rmdir "$DOCS_FOLDER" 2>/dev/null; fi
if [ "$pics_count" -eq 0 ]; then rmdir "$PICS_FOLDER" 2>/dev/null; fi
if [ "$videos_count" -eq 0 ]; then rmdir "$VIDEOS_FOLDER" 2>/dev/null; fi
if [ "$audio_count" -eq 0 ]; then rmdir "$AUDIO_FOLDER" 2>/dev/null; fi
if [ "$code_count" -eq 0 ]; then rmdir "$CODE_FOLDER" 2>/dev/null; fi
if [ "$archives_count" -eq 0 ]; then rmdir "$ARCHIVES_FOLDER" 2>/dev/null; fi
if [ "$torrents_count" -eq 0 ]; then rmdir "$TORRENTS_FOLDER" 2>/dev/null; fi
if [ "$apps_count" -eq 0 ]; then rmdir "$APPS_FOLDER" 2>/dev/null; fi
if [ "$others_count" -eq 0 ]; then rmdir "$OTHERS_FOLDER" 2>/dev/null; fi
# Summary
echo -e "\n${GREEN}โœ… Desktop organization complete!${NC}"
echo -e "${YELLOW}Summary:${NC}"
# Only show categories that were actually used
if [ $screenshots_count -gt 0 ]; then echo " ๐Ÿ“ธ Screenshots: $screenshots_count"; fi
if [ $screencasts_count -gt 0 ]; then echo " ๐ŸŽฅ Screencasts: $screencasts_count"; fi
if [ $docs_count -gt 0 ]; then echo " ๐Ÿ“„ Docs: $docs_count"; fi
if [ $pics_count -gt 0 ]; then echo " ๐Ÿ–ผ๏ธ Images: $pics_count"; fi
if [ $videos_count -gt 0 ]; then echo " ๐ŸŽฌ Videos: $videos_count"; fi
if [ $audio_count -gt 0 ]; then echo " ๐ŸŽต Audio: $audio_count"; fi
if [ $code_count -gt 0 ]; then echo " ๐Ÿ’ป Code: $code_count"; fi
if [ $archives_count -gt 0 ]; then echo " ๐Ÿ—œ๏ธ Archives: $archives_count"; fi
if [ $torrents_count -gt 0 ]; then echo " ๐Ÿ“ฅ Torrents: $torrents_count"; fi
if [ $apps_count -gt 0 ]; then echo " ๐Ÿ“ฑ Apps: $apps_count"; fi
if [ $others_count -gt 0 ]; then echo " ๐Ÿ“ฆ Other: $others_count"; fi
if [ $errors_count -gt 0 ]; then
echo -e " ${RED}โš ๏ธ Errors: $errors_count${NC}"
fi
total=$((screenshots_count + screencasts_count + docs_count + pics_count + videos_count + audio_count + code_count + archives_count + torrents_count + apps_count + others_count))
echo -e "\n${GREEN}Total files organized: $total${NC}"
echo -e "\nYour organized files are in: ~/Desktop/$MAIN_FOLDER"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment