如何清除Homebrew


Answers:


17

rm -rf不会询问您是否确定何时删除,因此请确保该cd命令可以使您离开/ tmp cd /tmp将您带到安全的地方,以防您一次性复制/粘贴所有内容,从而避免删除文件)从当前目录)

在您的终端中尝试以下操作:

cd /tmp
cd `brew --prefix`
rm -rf Cellar
brew prune
rm `git ls-files`
rm -r Library/Homebrew Library/Aliases Library/Formula Library/Contributions
rm -rf .git
rm -rf ~/Library/Caches/Homebrew

有关此主题的更多信息,请参见Homebrew FAQ


1
我会检查是否cd `brew --prefix`转到了您没有签入普通git文件的文件夹,因为旧的/发生故障的设置可能会失败,并且删除git ls-files可能会删除您的Brew其余部分。
bmike

我确实阅读了文档,只是认为这可能是一个有用的问题,可供将来参考。我的指令确实有问题,我将其发布为一个单独的问题:apple.stackexchange.com/questions/82863/…–
ipavlic

请注意,指向Homebrew常见问题解答的链接应从github.com/mxcl/homebrew/wiki/FAQ/…更新为github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/…(我不能编辑或添加评论)。
cloudnthings


5

虽然HomeBrew的安装位于其首页的醒目位置,但详细信息却没有。 https://brew.sh/ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 长期以来,很难找到可靠的卸载程序。现在,在文档中单击几下即可使用一种官方方法:https : //docs.brew.sh/FAQ 要卸载Homebrew,请将以下命令粘贴在终端提示中。 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"


3

这是删除Homebrew的更好的解决方案:https : //gist.github.com/SteveBenner/11254428

#!/usr/bin/env ruby
#
# Locates and removes Homebrew installation
# http://brew.sh/
#
# Author: Stephen Benner
# https://github.com/SteveBenner
#
require 'optparse'
require 'fileutils'
require 'open3'

$stdout.sync = true

# Default options
options = {
  :quiet     => false,
  :verbose   => true,
  :dry_run   => false,
  :force     => false,
  :find_path => false
}

optparser = OptionParser.new do |opts|
  opts.on('-q', '--quiet', 'Quiet mode - suppress output.') do |setting|
    options[:quiet]   = setting
    options[:verbose] = false
  end
  opts.on('-v', '--verbose', 'Verbose mode - print all operations.') { |setting| options[:verbose] = setting }
  opts.on('-d', '--dry', 'Dry run - print results, but perform no actual operations.') do |setting|
    options[:dry_run] = setting
  end
  opts.on('-f', '--force', 'Forces removal of files, bypassing prompt. USE WITH CAUTION.') do |setting|
    options[:force] = setting
  end
  opts.on('-p', '--find-path', 'Output homebrew location if found, then exit.') do |setting|
    options[:find_path] = setting
    options[:quiet]     = true
  end
  opts.on('-h', '--help', '--usage', 'Display usage info and quit.') { puts opts; exit }
end
optparser.parse!
$quiet = options[:quiet] # provides access to option value within methods

# Files installed into the Homebrew repository
BREW_LOCAL_FILES = %w[
  .git
  Cellar
  Library/brew.rb
  Library/Homebrew
  Library/Aliases
  Library/Formula
  Library/Contributions
  Library/LinkedKegs
]
# Files that Homebrew installs into other system locations
BREW_SYSTEM_FILES = %W[
  #{ENV['HOME']}/Library/Caches/Homebrew
  #{ENV['HOME']}/Library/Logs/Homebrew
  /Library/Caches/Homebrew
]
$files = []

# This function runs given command in a sub-shell, expecting the output to be the
# path of a Homebrew installation. If given a block, it passes the shell output to
# the block for processing, using the return value of the block as the new path.
# Known Homebrew files are then scanned for and added to the file list. Then the
# directory is tested for a Homebrew installation, and the git index is added if
# a valid repo is found. The function won't run once a Homebrew installation is
# found, but it will accumulate untracked Homebrew files each invocation.
#
# @param  [String] cmd       a shell command to run
# @param  [String] error_msg message to print if command fails
#
def locate_brew_path(cmd, error_msg = 'check homebrew installation and PATH.')
  return if $brew_location # stop testing if we find a valid Homebrew installation
  puts "Searching for homewbrew installation using '#{cmd}'..." unless $quiet

  # Run given shell command along with any code passed-in via block
  path = `#{cmd}`.chomp
  path = yield(path) if block_given? # pass command output to your own fancy code block

  begin
    Dir.chdir(path) do
      # Search for known Homebrew files and folders, regardless of git presence
      $files += BREW_LOCAL_FILES.select { |file| File.exist? file }.map {|file| File.expand_path file }
      $files += Dir.glob('**/{man,bin}/**/brew*')
      # Test for Homebrew git repository (use popen3 so we can suppress git error output)
      repo_name = Open3.popen3('git remote -v') do |stdin, stdout, stderr|
        stderr.close
        stdout.read
      end
      if repo_name =~ /homebrew.git|Homebrew/
        $brew_location = path
      else
        return
      end
    end
  rescue StandardError # on normal errors, continue program
    return
  end
end

# Attempt to locate homebrew installation using a command and optional code block
# for processing the command results. Locating a valid path halts searching.
locate_brew_path 'brew --prefix'
locate_brew_path('which brew') { |output| File.expand_path('../..', output) }
locate_brew_path 'brew --prefix' do |output|
  output = output.split($/).first
  File.expand_path('../..', output)
end

# Found Homebrew installation
if $brew_location
  puts "Homebrew found at: #{$brew_location}" unless options[:quiet]
  if options[:find_path]
    puts $brew_location
    exit
  end
  # Collect files indexed by git
  begin
    Dir.chdir($brew_location) do
      # Update file list (use popen3 so we can suppress git error output)
      Open3.popen3('git checkout master') { |stdin, stdout, stderr| stderr.close }
      $files += `git ls-files`.split.map {|file| File.expand_path file }
    end
  rescue StandardError => e
    puts e # Report any errors, but continue the script and collect any last files
  end
end

# Collect any files Homebrew may have installed throughout our system
$files += BREW_SYSTEM_FILES.select { |file| File.exist? file }

abort 'Failed to locate any homebrew files!' if $files.empty?

# DESTROY! DESTROY! DESTROY!
unless options[:force]
  print "Delete #{$files.count} files? "
  abort unless gets.rstrip =~ /y|yes/i
end

rm =
  if options[:dry_run]
    lambda { |entry| puts "deleting #{entry}" unless options[:quiet] }
  else
    lambda { |entry| FileUtils.rm_rf(entry, :verbose => options[:verbose]) }
  end

puts 'Deleting files...' unless options[:quiet]
$files.each(&rm)

1
欢迎询问不同!尽管您提供的链接可以回答问题,但最好在此处包括答案并提供链接以供参考。如果链接的页面发生更改,仅链接的答案可能会失效。我已经编辑了您的问题,以包括您认为所指的解决方案,但是,如果不是,请引用相关部分。另外,您能否解释为什么您的解决方案更好?
grg

1
它不止几行,所以我认为包括代码会很麻烦,但我很乐意将来这样做。最好是因为有些文件和目录没有被我的脚本删除的发布答案捕获。它通过CLI选项为用户提供实用程序,更详尽地搜索冲泡位置,并进行了编码,以便在您想要改进脚本时可以轻松进行修改。
史蒂夫·本纳2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.