首頁·終端機命令

base64

文字處理

用法

base64 [file]

把位元組轉成 ASCII 文字再轉回來,讓二進位能走只允許文字的通道;這是編碼不是加密,解碼選項在 GNU 是 -d,舊版 macOS 是 -D。

常用選項

選項含義
-dDecode. GNU and recent macOS take -d; older macOS needs -D
-wGNU: wrap at N columns, and -w0 keeps it on one line
-iGNU: ignore junk while decoding. On macOS -i names the input file
-oBSD and macOS: write to this file
-bBSD and macOS: wrap the encoded output at N characters

範例

base64 -w0 cert.pem

One long line, no wrapping (GNU).

echo aGVsbG8= | base64 -d

Decodes back to hello.

base64 -i photo.png -o photo.txt

Encoding into a file on macOS.

它們本來就是用管線串起來用的。串三條往往比讓一條命令做完所有事更短。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. base64 是做什麼的?

把位元組轉成 ASCII 文字再轉回來,讓二進位能走只允許文字的通道;這是編碼不是加密,解碼選項在 GNU 是 -d,舊版 macOS 是 -D。

Q. 怎麼寫?

base64 [file] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 5 個,完整列表在 man base64。這條命令屬於文字處理。

相關命令

man 手冊: man base64