如何通过命令行删除/bin目录二进制文件及Go编译文件残留问题
Hey there, let's break down your two questions with clear, actionable steps:
1. Deleting a Binary File from the /bin Directory
The /bin directory holds critical system binaries, so you'll need elevated privileges to modify it. Here's how to safely delete a file here:
- First, double-check the exact file path you want to remove (never guess—accidentally deleting a system binary like
lsorbashcan break your entire system!) - Run the
rmcommand withsudoto gain admin access:
For example, if you added a custom binary namedsudo rm /bin/your-target-binary-namemy-toolto/bin, you'd run:sudo rm /bin/my-tool - Critical note: Only delete files you've added or confirmed are non-essential to system operation.
2. Deleting the Go Binary Installed to $HOME/go/bin
The default go clean command only clears local build cache and temporary files from your project directory—it doesn't touch binaries installed to your GOBIN path. Here are two reliable ways to fix this:
Option 1: Directly Delete the Binary with rm
Since you know the file lives in $HOME/go/bin, simply use the rm command:
rm $HOME/go/bin/hello
You can verify the file exists first with:
ls $HOME/go/bin/hello
Option 2: Use go clean -i to Remove Installed Binaries
If you prefer using Go's built-in tools, add the -i flag to go clean—this tells Go to delete the installed binary alongside local build artifacts. Run this from your project directory:
cd $HOME/go/src/hello go clean -i
This will automatically remove the hello binary from your GOBIN (which is set to $HOME/go/bin in your setup, per your go env and .zshrc config).
Quick clarification: The -i flag stands for "install"—it reverses the go install action by removing the installed executable.
内容的提问来源于stack exchange,提问作者Dmitry S.




