使用脚本清除 VScode 中的老旧插件

使用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."

7 个赞

感谢分享

1 个赞

感谢大佬

谢谢大佬

好东西

收藏了,有用

有没有win可以用的

在线等热佬 win版脚本

5 个赞

插个眼

收藏

同等win

mark

等个win的

用GPT转化成powershell的呗

# Navigate to the Visual Studio Code extensions directory
$extensionsPath = "$env:USERPROFILE\.vscode\extensions"
cd $extensionsPath

# Get all extension directories
$extensionFolders = Get-ChildItem -Name

# Process each extension to identify and remove older versions
$extensionFolders | ForEach-Object {
    $extensionName = $_ -replace '-\d+\.\d+\.\d+$', '' # Remove version number from the name
    $versions = Get-ChildItem -Name | Where-Object { $_ -like "$extensionName-*" } | Sort-Object {[Version]($_ -replace "$extensionName-", '')}

    # Count how many versions exist
    $count = $versions.Count
    if ($count -gt 1) {
        # Remove all but the latest version
        $versions | Select-Object -First ($count - 1) | ForEach-Object {
            $oldVersionPath = Join-Path $extensionsPath $_
            Write-Host "Removing old version of $extensionName: $_"
            Remove-Item $oldVersionPath -Recurse -Force
        }
    }
}

Write-Host "Cleanup complete."

未经过测试