Skip to content
← 返回macOS列表
命令与观察

macOS:终端、会话与日常操作 ​

检查状态与定位问题 ​

uname(macOS) · 打印当前计算机及其上运行的操作系统的详细信息 ​

注意:有关操作系统的其他信息,请尝试使用 sw_vers 命令。 更多信息:https://keith.github.io/xcode-man-pages/uname.1.html。

  • 打印内核名称:
bash
uname
  • 打印系统架构和处理器信息:
bash
uname -mp
  • 打印内核名称、内核版本号和内核版本详细信息:
bash
uname -srv
  • 打印系统主机名:
bash
uname -n
  • 打印所有可用的系统信息:
bash
uname -a

uptime(macOS) · 告知当前系统运行多长时间和其他信息 ​

更多信息:https://keith.github.io/xcode-man-pages/uptime.1.html。

  • 打印当前时间,运行时间,登录用户数量和其他信息:
bash
uptime

环境、会话与自动执行 ​

defaults(macOS) · 读取和写入 macOS 应用程序的用户配置 ​

更多信息:https://keith.github.io/xcode-man-pages/defaults.1.html。

  • 读取应用程序选项的系统默认值:
bash
defaults read {{应用名}} {{选项}}
  • 读取应用程序选项的默认值:
bash
defaults read -app {{应用名}} {{选项}}
  • 写入应用程序选项的默认值:
bash
defaults write {{应用名}} {{选项}} {{- 类型}} {{值}}
  • 加速任务控制界面弹出动画(时间设置为 0.1):
bash
defaults write com.apple.Dock expose-animation-duration -float 0.1
  • 删除应用程序的所有默认值:
bash
defaults delete {{应用名}}

专用工具与其他操作 ​

arch(macOS) · 显示系统架构的名称,或者在不同的架构下运行命令 ​

另请参阅:uname。 更多信息:https://keith.github.io/xcode-man-pages/arch.1.html。

  • 显示系统的架构:
bash
arch
  • 使用 x86_64 来运行一个命令:
bash
arch -x86_64 {{命令}}

autoraise(macOS) · 当鼠标悬停在窗口上时,自动将其置顶和/或聚焦 ​

更多信息:https://github.com/sbmpost/AutoRaise。

  • 在后台运行 AutoRaise:
bash
autoraise &

cal(macOS) · 打印日历信息 ​

更多信息:https://keith.github.io/xcode-man-pages/cal.1.html。

  • 显示本月日历:
bash
cal
  • 显示上月、本月和下月的日历:
bash
cal -3
  • 显示指定月份的日历(月份为 1-12 月):
bash
cal -m {{月}}
  • 显示本年日历:
bash
cal -y
  • 显示指定年份的日历(年份为 4 个数字):
bash
cal {{年}}
  • 显示指定年月的日历:
bash
cal {{月}} {{年}}
  • 显示指定年份的复活节日期:
bash
ncal -e {{年}}

dark-mode(macOS) · 从命令行控制 macOS 的深色模式 ​

更多信息:https://github.com/sindresorhus/dark-mode。

  • 切换深色模式(如果目前是关闭的,就把它打开,如果目前是打开的,就把它关闭):
bash
dark-mode
  • 打开深色模式:
bash
dark-mode on
  • 关闭深色模式:
bash
dark-mode off
  • 检查深色模式是否开启:
bash
dark-mode status

date(macOS) · 设置或显示系统日期 ​

更多信息:https://keith.github.io/xcode-man-pages/date.1.html。

  • 使用默认区域设置的格式显示当前日期:
bash
date +%c
  • 以 UTC 和 ISO 8601 格式显示当前日期:
bash
date -u +%Y-%m-%dT%H:%M:%SZ
  • 将当前日期显示为 unix 时间戳(自 1970-01-01 00:00:00 以来的秒数):
bash
date +%s
  • 使用默认格式显示特定日期(格式化指定 UNIX 时间戳):
bash
date -r {{1473305798}}
  • 使用默认格式显示相对于当前日期的日期:
bash
date -v {{+1d}} -v {{-20m}}

dmesg(macOS) · 将内核消息写入标准输出 ​

更多信息:https://keith.github.io/xcode-man-pages/dmesg.8.html。

  • 显示内核消息:
bash
dmesg
  • 筛选内核日志中的 memory 文本;这不是实时可用内存统计:
bash
dmesg | grep -i memory
  • 一次显示一页内核消息:
bash
dmesg | less

drutil(macOS) · 与 DVD 刻录机交互 ​

更多信息:https://keith.github.io/xcode-man-pages/drutil.1.html。

  • 从驱动器中弹出磁盘:
bash
drutil eject
  • 将目录作为 iso9660 文件系统刻录到 DVD 上。完成后跳过验证并弹出:
bash
drutil burn -noverify -eject -iso9660 {{源目录}}

pbcopy(macOS) · 将来自标准输入的数据放入剪贴板 ​

相当于在键盘上按下 <Cmd c>。 更多信息:https://keith.github.io/xcode-man-pages/pbcopy.1.html。

  • 将文件的内容放入剪贴板:
bash
pbcopy < {{路径/到/文件}}
  • 将命令的执行结果放入剪贴板:
bash
find . -type f -name "*.png" | pbcopy

pbpaste(macOS) · 将剪贴板的内容发送到标准输出 ​

相当于在键盘上按下 <Cmd v>。 更多信息:https://keith.github.io/xcode-man-pages/pbcopy.1。

  • 将剪贴板的内容写入文件:
bash
pbpaste > {{路径/到/文件}}
  • 将剪贴板的内容用作命令的输入:
bash
pbpaste | grep foo

shuf(macOS) · 生成随机排列 ​

更多信息:https://manpagez.com/man/1/shuf/。

  • 随机化文件中的行顺序并输出结果:
bash
shuf {{文件名}}
  • 只输出结果的前 5 条:
bash
shuf --head-count=5 {{路径/到/文件}}
  • 将结果输出写入另一个文件:
bash
shuf {{路径/到/输入文件}} --output {{路径/到/输出文件}}
  • 生成范围(1-10)内的随机数:
bash
shuf --input-range=1-10

systemsetup(macOS) · 配置系统首选项计算机设置 ​

更多信息:https://support.apple.com/guide/remote-desktop/about-systemsetup-apd95406b8d/mac。

  • 启用远程登录(SSH):
bash
systemsetup -setremotelogin on
  • 指定时区、NTP 服务器并启用网络时间:
bash
systemsetup -settimezone "{{美国 / 太平洋}}" -setnetworktimeserver {{us.pool.ntp.org}} -setusingnetworktime on
  • 使机器从不休眠,并在电源故障或内核死机时自动重新启动:
bash
systemsetup -setsleep off -setrestartpowerfailure on -setrestartfreeze on
  • disks 选择启动:
bash
systemsetup -liststartupdisks
  • 指定新的启动盘:
bash
systemsetup -setstartupdisk {{路径}}

w(macOS) · 显示谁登录以及他们在做什么 ​

打印用户登录、tty、远程主机、登录时间、空闲时间、当前进程。 更多信息:https://keith.github.io/xcode-man-pages/w.1.html。

  • 显示登录用户信息:
bash
w
  • 显示没有标题的登录用户信息:
bash
w -h
  • 显示有关已登录用户的信息,按其空闲时间排序:
bash
w -i

示例整理自 tldr-pages,作者为 tldr-pages team and contributors,遵循 CC BY 4.0。

继续阅读