Created
November 27, 2024 06:08
-
-
Save danmackinlay/50bc5358f8c7eb16b13af5b23a34be54 to your computer and use it in GitHub Desktop.
delete all but the latest copies of VS Code on the remote server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# === VSCode Server Cleanup Script === | |
# Description: | |
# This script cleans up specified VSCode server directories by deleting all | |
# subdirectories except the two most recently modified ones. | |
# | |
# Usage: | |
# ./vscode_cleanup.sh [--dry-run] | |
# | |
# Options: | |
# --dry-run Perform a dry run without deleting any directories. | |
# | |
# Logs: | |
# Actions are logged to ~/vscode_cleanup.log | |
# Exit immediately if a command exits with a non-zero status | |
set -e | |
# Function to display usage information | |
usage() { | |
echo "Usage: $0 [--dry-run]" | |
echo | |
echo "Options:" | |
echo " --dry-run Perform a dry run without deleting any directories." | |
exit 1 | |
} | |
# Parse command-line arguments | |
dry_run=false | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
--dry-run) | |
dry_run=true | |
shift | |
;; | |
-*|--*) | |
echo "Unknown option: $1" | |
usage | |
;; | |
*) | |
echo "Unknown argument: $1" | |
usage | |
;; | |
esac | |
done | |
# Notify if dry run mode is enabled | |
if [ "$dry_run" = true ]; then | |
echo "=== Dry Run Mode Enabled: No directories will be deleted ===" | |
fi | |
# Array of directories to clean | |
directories=( | |
"$HOME/.vscode-server/bin/" | |
"$HOME/.vscode-server/cli/servers/" | |
) | |
# Log file path | |
log_file="$HOME/vscode_cleanup.log" | |
# Iterate over each target directory | |
for dir in "${directories[@]}"; do | |
# Check if the directory exists | |
if [ -d "$dir" ]; then | |
echo "Cleaning up directory: $dir" | tee -a "$log_file" | |
# Navigate to the directory | |
cd "$dir" || { echo "Failed to enter directory: $dir" | tee -a "$log_file"; continue; } | |
# Find all non-hidden subdirectories, sorted by modification time (newest first) | |
# and store them in an array | |
mapfile -t sorted_dirs < <( | |
find . -maxdepth 1 -mindepth 1 -type d ! -name ".*" -printf '%T@ %p\n' | | |
sort -nr | | |
awk '{print $2}' | |
) | |
# Check if there are more than two subdirectories | |
if [ "${#sorted_dirs[@]}" -le 2 ]; then | |
echo "No directories to delete in $dir. Only ${#sorted_dirs[@]} subdirectories present." | tee -a "$log_file" | |
cd - >/dev/null || exit | |
continue | |
fi | |
# Retain the two most recently modified directories | |
latest_dir1="${sorted_dirs[0]}" | |
latest_dir2="${sorted_dirs[1]}" | |
echo "Retaining the most recent directories:" | tee -a "$log_file" | |
echo "1. ${latest_dir1#./}" | tee -a "$log_file" | |
echo "2. ${latest_dir2#./}" | tee -a "$log_file" | |
# Directories to delete: all except the first two | |
dirs_to_delete=("${sorted_dirs[@]:2}") | |
if [ "${#dirs_to_delete[@]}" -gt 0 ]; then | |
if [ "$dry_run" = true ]; then | |
echo "=== Dry Run: The following directories would be deleted ===" | tee -a "$log_file" | |
for del_dir in "${dirs_to_delete[@]}"; do | |
# Extract modification time in human-readable format | |
mtime=$(stat -c '%y' "$del_dir" 2>/dev/null) | |
echo "Directory: ${del_dir#./} | mtime: $mtime" | tee -a "$log_file" | |
done | |
else | |
echo "Deleting the following directories:" | tee -a "$log_file" | |
for del_dir in "${dirs_to_delete[@]}"; do | |
# Extract modification time in human-readable format | |
mtime=$(stat -c '%y' "$del_dir" 2>/dev/null) | |
echo "Directory: ${del_dir#./} | mtime: $mtime" | tee -a "$log_file" | |
# Delete the directory | |
rm -rf -- "$del_dir" | |
done | |
fi | |
else | |
echo "No directories to delete in $dir." | tee -a "$log_file" | |
fi | |
# Return to the original directory | |
cd - >/dev/null || exit | |
else | |
echo "Directory does not exist: $dir" | tee -a "$log_file" | |
fi | |
done | |
# === End of VSCode Server Cleanup Script === |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment