# 文件名

文件名是[计算机文件](https://www.edgepedia.org/zh/computer-file)在文件系统中被识别所用的字符串，通常由基本名和可选扩展名组成，二者以句点分隔，例如 `report.pdf`。<sup>[1](https://rottenwifi.com/top-100-file-extensions-what-they-mean-and-which-apps-open-them/)</sup> 包括 Windows、macOS 和 Linux 在内的操作系统依靠扩展名来选择关联的应用程序和图标，但后缀并不保证文件的实际内容：把 `photo.jpg` 改为 `photo.pdf` 只改变标签，格式并未改变。<sup>[1](https://rottenwifi.com/top-100-file-extensions-what-they-mean-and-which-apps-open-them/)</sup><sup> • </sup><sup>[2](https://support.microsoft.com/en-US/Windows/Experience/Storage-FileManagement/common-file-name-extensions-in-windows)</sup>

| 关键事实 | 详情 |
|---|---|
| 结构 | 基本名加可选扩展名，以句点分隔；在 `expenses.xlsx` 中，基本名是 `expenses`，扩展名是 `xlsx`<sup>[2](https://support.microsoft.com/en-US/Windows/Experience/Storage-FileManagement/common-file-name-extensions-in-windows)</sup> |
| 扩展名不是证明 | 更改扩展名只改变名称，不改变格式；使用魔数字节和 MIME 检测等基于内容的检查来确认类型<sup>[2](https://support.microsoft.com/en-US/Windows/Experience/Storage-FileManagement/common-file-name-extensions-in-windows)</sup><sup> • </sup><sup>[3](https://openfiletools.com/guides/how-to-identify-a-file-type)</sup> |
| Windows 限制 | 默认最大路径长度为 260 个字符（MAX_PATH）；保留字符包括 `<`、`>`、`:`、`"`、`/`、`\`、竖线、`?`、`*`；保留设备名包括 CON、NUL、COM1 到 COM9，以及 LPT1 到 LPT9<sup>[4](https://learn.microsoft.com/windows/desktop/FileIO/naming-a-file)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file)</sup> |
| 8.3 遗留格式 | 基本名为 1 到 8 个 ASCII 字符，扩展名为 1 到 3 个字符，不含空格，且最多一个句点<sup>[6](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/18e63b13-ba43-4f5f-a5b7-11e871b71f14)</sup> |
| Linux 限制 | 只有两个禁止字节：正斜杠和空字节；在 Ext4 上，文件名限制为 255 字节，完整路径限制为 4,096 字节<sup>[7](https://www.man7.org/linux/man-pages/man7/filename.7.html)</sup><sup> • </sup><sup>[8](https://www.netpump.com/assets/other/netpump-data-administration-guide.pdf)</sup> |
| 大小写行为 | 只有 FAT 短文件名以大写存储名称；FAT 长文件名和 exFAT 保留大小写，而 NTFS 和苹果公司（Apple）的 HFS Plus 保留大小写但不区分大小写地匹配；许多 Unix 文件系统将 `example.txt` 和 `Example.txt` 视为不同文件<sup>[9](https://www.rfc-editor.org/rfc/rfc8493.html)</sup><sup> • </sup><sup>[20](https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file)</sup> |
| 保存建议 | 档案工作者建议使用 YYYYMMDD 形式的日期、不含空格、使用 ASCII 字符、用零填充的序号，以及约 25 个字符或更少的名称<sup>[10](https://www.archives.gov/files/preservation/formats/pdf/naming-and-organizing-files.pdf)</sup> |

## 文件名的构成

文件名有两个约定俗成的部分。基本名向用户标识文件，扩展名即最后一个句点之后的后缀，向操作系统表明文件类型。当用户双击 `document.pdf` 时，操作系统识别出 `.pdf` 扩展名并启动默认 PDF 查看器。<sup>[11](https://asic.gov.au.extensionfile.net/information/what-are-file-extensions)</sup> 扩展名还决定文件资源管理器显示哪个图标；`docx` 扩展名告诉 Windows，[Microsoft Word](https://www.edgepedia.org/zh/microsoft-word) 可以打开该文件。<sup>[2](https://support.microsoft.com/en-US/Windows/Experience/Storage-FileManagement/common-file-name-extensions-in-windows)</sup>

扩展名是提示，不是保证。它可能缺失、具有误导性或错误，而像 `data` 这样的结果或通用 MIME 类型可能无法识别确切格式。<sup>[12](https://www.itechguides.com/how-to-manually-find-programs-that-can-open-a-file-type/)</sup> 更强的识别方式是将扩展名与文件内部字节签名（即所谓的魔数字节）进行比较，而最有力的实用检查是看能识别格式的解析器能否安全读取其结构。<sup>[3](https://openfiletools.com/guides/how-to-identify-a-file-type)</sup> 在取证场景中，这一差距在另一个方向上也很重要：重命名扩展名是隐藏文件类型的简便方法，而一旦扩展名被更改，基本识别就会失败。<sup>[13](https://www.sciencedirect.com/science/article/pii/S2666281723000835)</sup>

有些名称带有 `.tar.gz` 这样的复合结尾，一次描述两层格式。<sup>[12](https://www.itechguides.com/how-to-manually-find-programs-that-can-open-a-file-type/)</sup> 像 `.dat`（表示应用程序数据）或 `.bak`（表示备份副本）这样的通用扩展名完全不能标识底层格式；创建该文件的应用程序通常是判断依据。<sup>[1](https://rottenwifi.com/top-100-file-extensions-what-they-mean-and-which-apps-open-them/)</sup>

## Windows 的命名规则

Windows 的命名规则源自 MS-DOS。盘符、反斜杠路径分隔符和保留设备名可追溯到 1981 年，当时文件名是八个字符加三个字母的扩展名；为了兼容数十年来已发布的软件，这些规则仍被强制执行。<sup>[14](https://hexaqa.com/reference)</sup>

**保留字符和名称。** Windows 禁止在文件和目录名中使用九个字符：`<`、`>`、`:`、`"`、`/`、`\`、`|`、`?` 和 `*`，以及 ASCII NUL 字符和整数值为 1 到 31 的字符。<sup>[5](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file)</sup> 它还保留以下设备名：CON、PRN、AUX、NUL、COM1 到 COM9，以及 LPT1 到 LPT9，包括用上标数字 ¹、² 和 ³ 书写的变体。像 `NUL.txt` 这样的名称等同于 `NUL`，而 `echo test > COM¹` 无法创建文件。<sup>[5](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file)</sup> 文件或目录名不得以空格或句点结尾，但允许以句点开头，例如 `.temp`。<sup>[5](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file)</sup> 创建名称时，对象管理器还会去除首尾的 ASCII 空格字符（0x20）。<sup>[15](https://learn.microsoft.com/en-us/troubleshoot/windows-client/shell-experience/file-folder-name-whitespace-characters)</sup>

**长度限制。** 在 [Windows 10](https://www.edgepedia.org/zh/windows-10) 版本 1607 之前的 Windows 版本中，最大路径长度为 MAX_PATH，定义为 260 个字符；更高版本可以通过注册表项或[组策略](https://www.edgepedia.org/zh/group-policy)移除此限制。<sup>[4](https://learn.microsoft.com/windows/desktop/FileIO/naming-a-file)</sup> 通过 API 创建目录时，路径还必须留出附加 8.3 名称的空间，因此目录名不能超过 MAX_PATH 减去 12。<sup>[16](https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell)</sup>

**8.3 遗留格式。** 8.3 文件名又称 DOS 名或短名，必须只包含低于 0x80 的 ASCII 字符，不含空格，且最多一个句点；基本名为 1 到 8 个字符，扩展名（如果有）为 1 到 3 个字符。<sup>[6](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/18e63b13-ba43-4f5f-a5b7-11e871b71f14)</sup> 创建长名称时，Windows 还可能生成一个短的 8.3 别名，通常包含波浪号（~），并将两者都存储在磁盘上；出于性能考虑可以禁用这种别名机制，因此应用程序不应假定该别名存在。<sup>[5](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file)</sup> 在 NTFS、exFAT、UDFS 和 FAT32 上，长名称以 Unicode 存储，因此原始长名称始终得以保留，文件可以在 NTFS 和 FAT 分区之间复制而不丢失名称信息。<sup>[5](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file)</sup>

## Linux 与 POSIX 系统的规则

Linux 继承了 Unix 的最小规则：文件名就是一个字节序列。内核将路径名存储为 C 字符串，因此恰好有两个值被禁止：正斜杠（0x2F），用于分隔路径组成部分；以及空字节，用于终止字符串。<sup>[7](https://www.man7.org/linux/man-pages/man7/filename.7.html)</sup><sup> • </sup><sup>[14](https://hexaqa.com/reference)</sup> 路径名最长可为 PATH_MAX 字节，文件名最长可为 NAME_MAX 字节，该值因文件系统而异。<sup>[7](https://www.man7.org/linux/man-pages/man7/filename.7.html)</sup><sup> • </sup><sup>[17](https://www.man7.org/linux/man-pages/man0/limits.h.0p.html)</sup> 在 Ext4 上，这相当于文件名限制为 255 字节，完整路径限制为 4,096 字节，而 Windows 的合计限制为 260 个字符。<sup>[8](https://www.netpump.com/assets/other/netpump-data-administration-guide.pdf)</sup>

由于除两个禁止值之外的字节都允许，Linux 文件名可以包含换行符和其他控制字符。POSIX.1-2024 鼓励实现禁止包含换行符的文件名，但 Linux 并未遵循这一建议。<sup>[7](https://www.man7.org/linux/man-pages/man7/filename.7.html)</sup> 为了实现最大互操作性，POSIX 定义了可移植文件名字符集，包括字母 A 到 Z 和 a 到 z、数字 0 到 9，以及句点、下划线和连字符；仅由这些字符构成的路径名在所有受支持的区域设置中都可以作为字符串使用。<sup>[18](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)</sup>

## 大小写敏感性与 Unicode

操作系统对大小写的处理并不一致。由于 FAT 只将短（8.3）文件名转换为大写并保留长文件名的大小写，而 exFAT 不区分大小写且保留大小写，`example.txt` 并不总是存储为 `EXAMPLE.TXT`。<sup>[20](https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file)</sup> 许多 Unix 文件系统完全按提供的内容保存名称，允许 `example.txt` 和 `Example.txt` 作为不同文件共存。NTFS 和苹果公司的 [HFS Plus](https://www.edgepedia.org/zh/hfs-plus) 在存储文件时通常保留大小写，但在检索时不区分大小写，因此保存为 `Example.txt` 的文件也可以用 `EXAMPLE.TXT` 或 `example.txt` 检索到。<sup>[9](https://www.rfc-editor.org/rfc/rfc8493.html)</sup> Windows 记录了这一实际后果：应用程序不应假定大小写敏感，并应将 OSCAR、Oscar 和 oscar 视为同一名称，即使符合 POSIX 的文件系统可能将它们视为不同名称。<sup>[5](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file)</sup>

Unicode 增加了第二个分歧维度，因为有些字符有多种有效编码。苹果公司的 HFS Plus 会依据 Unicode 2.0 规范将文件名规范化为完全分解形式，而 Windows 将文件名视为不透明序列，并按提供的内容原样存储编码后的字节；Linux 的行为类似，不过具体行为取决于文件系统。<sup>[9](https://www.rfc-editor.org/rfc/rfc8493.html)</sup> 结果是一种熟悉的故障模式：在 Mac 上保存的 `café.txt` 可能与在 Windows 上键入的同一名称不匹配，从而对用户看得见的名称产生“找不到文件”。<sup>[14](https://hexaqa.com/reference)</sup> 大小写折叠也会与规范化相互作用：带有开尔文符号（U+212A）的文件名 `temp_200K` 与 `temp_200k` 在 NTFS 和 APFS 上被视为相同，但在不区分大小写查找下，在 ZFS 上则被视为不同，因此将两者从 ZFS 复制到 NTFS 会发生冲突，只有一个文件能保留下来。<sup>[19](https://www.cs.ucr.edu/~trentj/papers/fast23.pdf)</sup> BagIt 规范建议各实现不鼓励创建名称仅大小写不同的文件，并防止创建名称仅规范化形式不同的文件，且对两个名称应用相同规范化后再进行比较。<sup>[9](https://www.rfc-editor.org/rfc/rfc8493.html)</sup>

## 选择文件名

档案管理建议趋于一致：名称应在各系统之间保持有意义且对机器安全。美国国家档案与记录管理局（U.S. National Archives and Records Administration）借鉴史密森尼学会（Smithsonian）的数据管理实践，建议包含 YYYYMMDD 格式的创建日期（当按时间排序很重要时，以它作为名称开头）、项目名称或缩写、诸如 `v` 或 `_draft` 的版本标识，以及用零填充的序号；至少使用两个零，并根据预期文件数量使用足够多的零，例如预期有 1,200 个文件时使用 `_0001`。<sup>[10](https://www.archives.gov/files/preservation/formats/pdf/naming-and-organizing-files.pdf)</sup> 它还建议省略空格以及连字符和下划线以外的标点，只使用 ASCII 字母数字字符，将名称限制在约 25 个字符以内，并让扩展名与实际文件格式相匹配。<sup>[10](https://www.archives.gov/files/preservation/formats/pdf/naming-and-organizing-files.pdf)</sup>

两个习惯可以降低日常风险。第一，在打开附件之前显示完整文件名（包括扩展名），不要双击意外的可执行文件、脚本、安装程序、启用宏的文件、注册表文件、包文件或磁盘映像文件。<sup>[1](https://rottenwifi.com/top-100-file-extensions-what-they-mean-and-which-apps-open-them/)</sup> 在 Windows [文件资源管理器](https://www.edgepedia.org/zh/file-explorer)中，可在“查看”选项卡的“显示/隐藏”组中勾选“文件扩展名”来显示扩展名。<sup>[2](https://support.microsoft.com/en-US/Windows/Experience/Storage-FileManagement/common-file-name-extensions-in-windows)</sup> 第二，当文件无法打开时，把扩展名、文件类型检测器和默认应用当作线索而非证据，并在软件发布者的文档中确认是否确切支持该格式；在可行时，在副本上进行测试。<sup>[12](https://www.itechguides.com/how-to-manually-find-programs-that-can-open-a-file-type/)</sup>

## References

1. [Top 100 File Extensions: What They Mean and Which Apps Open Them, RottenWiFi](https://rottenwifi.com/top-100-file-extensions-what-they-mean-and-which-apps-open-them/)
2. [Common file name extensions in Windows, Microsoft Support](https://support.microsoft.com/en-US/Windows/Experience/Storage-FileManagement/common-file-name-extensions-in-windows)
3. [How to Identify a File Type with Magic Bytes, MIME and Extension, OpenFileTools](https://openfiletools.com/guides/how-to-identify-a-file-type)
4. [Naming Files, Paths, and Namespaces, Win32 apps, Microsoft Learn](https://learn.microsoft.com/windows/desktop/FileIO/naming-a-file)
5. [Naming Files, Paths, and Namespaces, Win32 apps, Microsoft Learn (en-gb)](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file)
6. [MS-FSCC: 8.3 Filename, Microsoft Learn](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/18e63b13-ba43-4f5f-a5b7-11e871b71f14)
7. [pathname(7), Linux manual page](https://www.man7.org/linux/man-pages/man7/filename.7.html)
8. [NetPump Data Administration Guide, section 11.3.3](https://www.netpump.com/assets/other/netpump-data-administration-guide.pdf)
9. [RFC 8493: The BagIt File Packaging Format, IETF](https://www.rfc-editor.org/rfc/rfc8493.html)
10. [Best Practices for File Naming and Organizing, U.S. National Archives](https://www.archives.gov/files/preservation/formats/pdf/naming-and-organizing-files.pdf)
11. [What Are File Extensions?, ExtensionFile](https://asic.gov.au.extensionfile.net/information/what-are-file-extensions)
12. [How to Manually Find Programs That Can Open a File Type, iTechGuides](https://www.itechguides.com/how-to-manually-find-programs-that-can-open-a-file-type/)
13. [File type identification tools for digital investigations, Digital Forensics (ScienceDirect)](https://www.sciencedirect.com/science/article/pii/S2666281723000835)
14. [Filename and file compatibility across Windows, Mac and Linux, HEXAQA](https://hexaqa.com/reference)
15. [Support for Whitespace Characters in File and Folder Names, Microsoft Learn](https://learn.microsoft.com/en-us/troubleshoot/windows-client/shell-experience/file-folder-name-whitespace-characters)
16. [Maximum Path Length Limitation, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell)
17. [limits.h(0p), Linux manual page](https://www.man7.org/linux/man-pages/man0/limits.h.0p.html)
18. [POSIX.1-2017 Definitions, The Open Group](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)
19. [Name confusions in file systems (FAST 2023), UC Riverside](https://www.cs.ucr.edu/~trentj/papers/fast23.pdf)
20. [Naming Files, Paths, and Namespaces - Win32 apps](https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/filename

English original: https://www.edgepedia.org/filename
