使用vscode时,经常会在插件升级之后遗留很多个旧版本的插件,比如说copilot,经常会出现4,5个版本都存在本地目录的情况。整了个 shell 脚本清理老旧插件:
#!/bin/bash
# Change to the extensions directory
cd ~/.vscode/extensions || exit
# List all extensions, sort them by name and version, then reverse sort to get highest version first
# Finally, use awk to output extension names without version numbers
# uniq -u will filter out all extensions where there are duplicates, leaving only unique lines
# Those are the ones with only one version and don't need processing
#older_extensions=$(ls | sort -t. -k1,1 -k2,2V | tac | awk -F'-' '{print $1}' | uniq -d)
#
## not valid on mac
## Loop over the list of extension names that have more than one version installed
#for ext in $older_extensions; do
# # Find all versions of this extension and skip the first one (the latest one)
# ls | grep "^$ext" | head -n -1 | while read -r old_version; do
# echo "Removing old version of $ext: $old_version"
# # rm -rf "$old_version"
# echo "to removed $old_version"
# done
#done
# Get list of extension folders without the version numbers
extensions=$(ls | rev | cut -d'-' -f2- | rev | uniq)
for ext in $extensions; do
# Get a list of directories for each extension, sort them to have the latest version last
versions=$(ls | grep "^$ext" | sort -t. -k2,2V)
count=$(echo "$versions" | wc -l)
# If there's more than one version, then remove the older ones
if [ "$count" -gt 1 ]; then
# Remove all versions except the latest
echo "$versions" | head -n $((count - 1)) | while read -r old_version; do
echo "Removing old version of $ext: $old_version"
#echo "to removed $old_version"
rm -rf "$old_version"
done
fi
done
echo "Cleanup complete."