首页·终端命令

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