Mac¶
终端¶
ls /
"""
AppleInternal System bin etc private usr
Applications Users cores home sbin var
Library Volumes dev opt tmp
"""
lsof -i:3031 # 查看端口号是否占用
kill [-9] 3031 # 杀死进程,-9强制杀死
launchctl # 类似Linux的systemctl
which xx # 查找
unzip xxx.zip # 解压
CLT¶
安装Homebrew等工具会提示先安装CLT
Command Line Tools,是可独立Xcode下载安装的小型工具包,包含了macOS SDK和一些常用的命令行工具,比如git、clang等
设置环境变量¶
echo $PATH # 查看
export PATH=$PATH:xxx >> ~/.bash_profile # 新增,也可以直接编辑这个文件
source ~/.bash_profile # 重新加载
环境变量文件按加载顺序依次为
/etc/profile
/etc/paths
~/.bash_profile # 有这个文件后面的就不读了
~/.bash_login
~/.profile
~/.bashrc
设置默认SHELL¶
查看默认shell:echo $SHELL
Mac系统从macOS Catalina版开始默认shell为zsh,可以切换为bash,但由于GPLv3的原因,Mac自带的bash还是十几年前的3.2版本,所以需要先升级一下,即下载新版bash并设为默认
# 安装新版
brew install bash
# 设置交互shell为新版
chsh -s /usr/local/bin/bash
# 改回旧版:chsh -s / bin / bash
# 改回zsh:chsh -s / bin / zsh
# 设置登录shell为新版
# 即把/usr/local/bin/bash填加到当前系统受信任shells配置文件中,
sudo vim /etc/shells # 需要管理者权限
- 查看bash路径:
which -a bash
- 新版:
/usr/local/bin/bash
- 旧版:
/bin/bash
- 查看当前交互shell版本:
bash --version
- 查看默认交互shell版本:
echo $BASH_VERSION
tips:
低旧版本的bash依然存在于
bin/bash
下,为了保证系统的完整性,不建议删除。编写shell脚本如果想用新bash解释,记得要把
#!/bin/bash
要改为#!/usr/local/bin/bash
Homebrew¶
- Homebrew安装路径:
/usr/local/Homebrew/
(Intel版Mac),/opt/homebrew
(M1版) - Homebrew安装的软件路径:
/usr/local/Cellar/
路径(M1:/opt/homebrew/Cellar
),并自动在/usr/local/bin/
路径下创建link
其中Cellar译为酒窖,而Homebrew官方预先编译好的软件叫做Bottle(酒瓶子),Bottles就是很多酒瓶子(即软件),Homebrew把安装一个软件叫做把一个酒瓶子pour(倒入)到酒窖里。
由四部分组成
- Homebrew 源代码仓库
- Homebrew-core Homebrew 核心源
- Homebrew-cask 提供macos应用和大型二进制文件的安装
- Homebrew-bottles 预编译二进制软件包
# 官方下载(很慢,建议开全局代理)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
# 镜像下载
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
"""
M1安装完后会提示配置环境变量
==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/chonge/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"
"""
# 卸载,卸载不干净再装会报错
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
更换下载源¶
查看有哪些软件下载源:brew tap
# 更换brew.git源,主要用于更新brew本身
# 切换到Homebrewan路径:/usr/local/Homebrew/
cd $(brew --repo) # 等同于:git -C "$(brew --repo)"
git remote -v # 查看远程库信息
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git # 换成镜像源
git remote set-url origin https://github.com/Homebrew/brew.git # 换回官方源
# Formula源,用于brew下载安装软件的库,以前是和brew库在一起,但后来分开了
# 更换homebrew-core,主要安装一些开发者用的无界面程序,由官方维护,此外还有一些第三方维护的库
# 切换到 /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
cd $(brew --repo homebrew/core)
git remote -v # 查看远程库信息
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git # 换成镜像源
git remote set-url origin https://github.com/Homebrew/homebrew-core.git # 换回官方源
# 设置 homebrew-bottles 环境变量
# Formula源存储的只是软件的名称和下载地址等,真正要去下载预编译二进制软件包是在homebrew-bottles源
# 如果要恢复官方源,只需要删除或注释掉这个环境变量即可
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
# 更换homebrew-cask源(可选),主要安装一些AppStore中没有的GUI程序
# 切换到:/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask
cd $(brew --repo homebrew/cask)
git remote -v # 查看远程库信息
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git # 换成镜像源
git remote set-url origin https://github.com/Homebrew/homebrew-cask.git # 换回官方源
# homebrew-cask扩展homebrew-cask-versions(可选)
cd $(brew --repo)/Library/Taps/homebrew/homebrew-cask-versions
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask-versions.git # 换成镜像源
git remote set-url origin https://github.com/Homebrew/homebrew-cask-versions.git # 换回官方源
常用命令¶
brew help # 查看帮助
brew config # 查看配置
brew update # 更新brew本身
brew doctor # 检查问题
brew search xxx # 搜索想要安装的软件
brew info xxx # 查看想安装或已安装软件的信息
brew install xxx # 安装,默认会先检查升级brew本身,可以Ctrl+c跳过
brew uninstall xxx # 卸载
brew autoremove # 删除依赖项
brew cleanup # 清理旧版本和缓存
brew list [--casks] # 查看已安装软件列表
brew deps --installed --tree # 查看已安装软件列表,显示为依赖树的形式
brew leaves # 列出未被依赖的包
brew outdated # 查看哪些软件可更新,先更新brew
brew upgrade # 更新全部可更新软件
brew upgrade xxx # 更新xxx
# 查看安装的服务列表
brew services list
"""
Name Status User File
rabbitmq none
redis started /opt/homebrew/opt/redis/homebrew.mxcl.redis.plist
"""
# 大部分需要在后台运行的软件服务,都可以使用以下方式启动/启动并加到开机启动/停止/重启
brew services run/start/stop/restart xxx
brew services info redis
"""
redis (homebrew.mxcl.redis)
Running: ✔
Loaded: ✔
Schedulable: ✘
User:
PID: 43685
"""
其它¶
快捷键¶
- 显示隐藏文件:
Cmd + Shift + .
- 切换半角和圆角:
Shift + Space
- 清空命令行:
Cmd + k
开启三指拖移¶
SIP¶
SIP(系统完整性保护)安全机制:即使是以root身份也无法删除某些程序,比如/bin
下的,尽管你可以SIP禁用后删除,但也不建议这样去做。
最后更新:
2022-08-12