這是用戶在 2024-12-20 8:34 為 https://weiyen.net/articles/useful-macos-cmd-line-utilities/ 保存的雙語快照頁面,由 沉浸式翻譯 提供雙語支持。了解如何保存?

Useful built-in macOS command-line utilities
有用的內建 macOS 指令行公用程式

macOS comes with a lot of built-in utilities. Here's a list of some that I find interesting.
macOS 內建了許多公用程式。這裡列出了一些我覺得有趣的工具。

Sometimes when I'm bored, I like to look at the list of macOS Bash commands. Here's some commands that I found interesting:
有時候無聊時,我喜歡看看 macOS Bash 指令清單。以下是一些我覺得有趣的指令:

Access your Keychain programmatically
以程式化方式存取您的鑰匙鏈

If you store your secrets in the Keychain (and you should!), you can access them programmatically using security.
如果您將祕密儲存在 Keychain 中 (您應該這樣做!),您可以使用 security 程式化存取它們。

security find-internet-password -s "https://example.com"

I found this useful for writing automated scripts that used locally-stored credentials.
我發現這對於撰寫使用本機儲存憑證的自動化腳本非常有用。

Link: https://ss64.com/mac/security.html
連結:https://ss64.com/mac/security.html

Bonus tip: If you are using 1Password, there is a 1Password CLI that you can use to access your 1Password items from the command line.
額外提示:如果您使用 1Password,有一個 1Password CLI 可以用來從命令列存取您的 1Password 項目。

Open files from the terminal
從終端開啟檔案

If you want to open a file from the terminal, you can use the open command.
如果要從終端開啟檔案,可以使用 open 指令。

open file.txt

This will open the file in the default application for that file type, as if you had double-clicked it in the Finder.
這會在該檔案類型的預設應用程式中開啟檔案,就像您在 Finder 中雙擊該檔案一樣。

Link: https://ss64.com/mac/open.html
連結:https://ss64.com/mac/open.html

Copy and paste  複製並貼上

pbcopy and pbpaste are command-line utilities that allow you to copy and paste text to the pasteboard (what other operating systems might call the "clipboard").
pbpaste 是命令列工具,可讓您複製和貼上文字到貼紙板 (其他作業系統可能稱之為「剪貼簿」)。

pbcopy takes whatever was given in the standard input, and places it in the pasteboard.
pbcopy 擷取標準輸入的內容,並將其放入貼板。

echo "Hello, world!" | pbcopy;

pbpaste takes whatever is in the pasteboard and prints it to the standard output.
pbpaste 取得貼板中的任何內容,並將其列印到標準輸出。

pbpaste
>> Hello, world!

This is very useful for getting data from files into the browser, or other GUI applications.
這對於從檔案取得資料到瀏覽器或其他 GUI 應用程式非常有用。

Links:  連結:

UTC date  UTC 日期

If you work with servers a lot, it can be useful to know the current time in UTC, when e.g. looking at server logs.
如果您經常使用伺服器,在查看伺服器日誌等時,知道目前的 UTC 時間可能會很有用。

This is a one-liner in the terminal:
這是終端機中的單行字:

date -u

Alternatively, you can use
或者,您可以使用

TZ=UTC date

Link: https://ss64.com/mac/date.html
連結:https://ss64.com/mac/date.html

Internet speedtest  網速測試

If you want to run an Internet speedtest, you can run one directly from the terminal with
如果您要執行網際網路速度測試,可以直接從終端機執行,使用

networkQuality  # Note the capital "Q"!

Link: https://ss64.com/mac/networkquality.html
連結:https://ss64.com/mac/networkquality.html

Prevent your Mac from sleeping
防止 Mac 休眠

If you want to keep your Mac from sleeping, you can run caffeinate in the terminal.
如果您想讓 Mac 不睡覺,可以在終端機執行 caffeinate

caffeinate

caffeinate will keep your Mac awake until you stop it, e.g. by pressing Ctrl+C. caffeinate used to be a third-party tool, but it is now built-in to macOS.
caffeinate 會讓您的 Mac 保持清醒,直到您按下 Ctrl+C 停止它為止。caffeinate 過去是第三方工具,但現在已內建於 macOS。

I use this mostly to prevent my Mac from sleeping when I am running a server.
我主要用來防止 Mac 在執行伺服器時休眠。

Link: https://ss64.com/mac/caffeinate.html
連結:https://ss64.com/mac/caffeinate.html

Generate UUIDs  產生 UUID

If you need to generate a UUID, you can use the uuidgen command.
如果您需要產生 UUID,可以使用 uuidgen 指令。

uuidgen

By default uuidgen outputs a UUID in uppercase. You can combine this with tr and pbcopy to copy the UUID to the clipboard in lowercase.
預設 uuidgen 會輸出大寫的 UUID。您可以結合 pbcopy 將 UUID 以小楷複製到剪貼簿。

uuidgen | tr '[:upper:]' '[:lower:]' | pbcopy

I use this a lot when writing unit tests that require IDs.
我在撰寫需要 ID 的單元測試時,經常使用這個功能。

Link: https://ss64.com/mac/uuidgen.html
連結:https://ss64.com/mac/uuidgen.html

Honourable mentions  優異獎

  • mdfind: Spotlight search, but in the terminal. I generally use Spotlight itself (or rather the excellent Raycast). Link
    mdfind: Spotlight 搜尋,但在終端機。我一般使用 Spotlight 本身 (或更確切地說是出色的 Raycast) 。Link
  • say: This command makes your Mac speak the text you give it. Link
    say: 此指令可讓您的 Mac 說出您給予它的文字。Link
  • screencapture: This command allows you to take screenshots and save them to a file. I prefer using cmd-shift-5 for this. Link
    screencapture: 此指令允許您截取螢幕畫面並儲存到檔案。我比較喜歡使用 cmd-shift-5 來執行。Link
  • networksetup: This command allows you to configure your network settings programmatically. I found its API very intimidating, and so I haven't really used it much. Link
    networksetup: 此指令允許您以程式化的方式設定網路設定。我發現它的 API 非常嚇人,所以我沒怎麼用過它。Link

Update [9 Nov 2024]  更新 [2024 年 11 月 9 日]

After posting this article on Hacker News and making it to #2 on the front page(!), the folks over there suggested many many more interesting utilities.
many 更有趣的公用程式。

Here are some of my favourites:
以下是我的一些最愛:

  • sips: The Scriptable Image Processing System, for converting between various image formats. Link
    sips: Scriptable Image Processing System,用於在各種影像格式之間轉換。Link
  • afinfo: To probe metadata of audio files. Link
    afinfo: 探測音訊檔案的 metadata。Link
  • mdls: To probe metadata of all kinds of files. Link
    mdls: 探測各種檔案的 metadata。Link
  • afconvert: For converting between various audio formats. Link
    afconvert:用於在各種音訊格式之間轉換。Link
  • diskutil: For managing disk volumes, and a solid alternative to the built-in"Disk Utility" app. Link
    diskutil:用於管理磁碟區,是內建「Disk Utility」應用程式的可靠替代方案。Link
  • powermetrics: For monitoring system power consumption. Link
    powermetrics:用於監控系統功耗。Link
  • pmset: For power management tasks, e.g. to automatically turn your Mac on or off. Link
    pmset:用於電源管理任務,例如自動開啟或關閉 Mac。Link
  • dot_clean: Removes dot_underscore files. Useful when sharing files with non-Mac machines. Link
    dot_clean:移除 dot_underscore 檔案。與非 Mac 機器共用檔案時非常有用。Link

(Apologies if I missed your suggestion, there were just so many!)
(如果我錯過了您的建議,請見諒,因為太多了!)。

There are also a couple of other people who made similar lists that are worth checking out, e.g.
還有其他一些人也列出了類似清單,值得一試,例如


Like this article? Follow me on Bluesky or subscribe to the RSS feed to get notified about new articles.
喜歡這篇文章嗎?在 RSS feed 以獲得新文章的通知。
set 限製解除