Linux bash中文件名长度的限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6571435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limit on file name length in bash
提问by Sriram
The following questions are meant for bash and linux only:
以下问题仅适用于 bash 和 linux:
- Is there a limit on the number of characters in the absolute path name of a file?
- Is there a limit on the number of characters for the filename (without extension) only?
- 文件的绝对路径名中的字符数有限制吗?
- 文件名(无扩展名)的字符数是否有限制?
If so, what might these limits be? How can I access them in case they are system specific?
如果是这样,这些限制可能是什么?如果它们是特定于系统的,我如何访问它们?
采纳答案by e-satis
It depends very much on the filesystem. For the ext FS (currently the most used on Linux):
这在很大程度上取决于文件系统。对于 ext FS(目前在 Linux 上使用最多):
- max filename length: 255 bytes
- max path length: none
- 最大文件名长度:255 字节
- 最大路径长度:无
The extension is not something the FS is aware of, it 255 bytes, extension included (you can have file names without any extensions).
扩展名不是 FS 知道的,它有 255 个字节,包括扩展名(您可以拥有没有任何扩展名的文件名)。
Hereis a more exhaustive list of these limits, per FS.
这是每个 FS 的这些限制的更详尽列表。
There can also be extensions to your file system that can change your maximum length as well. For example, eCryptFS which uses part of the lower file name to keep metadata and limits the file name to a maximum length of 143 characters. See Ubuntu eCryptFS launchpadentry.
你的文件系统也可以有扩展,也可以改变你的最大长度。例如,eCryptFS 使用较低文件名的一部分来保留元数据并将文件名限制为最大长度为 143 个字符。请参阅Ubuntu eCryptFS 启动板条目。
回答by Eugene Yarmash
It depends on the filesystem used. For example, ext4 has maximum filename length of 256 bytes and unlimited pathname length.
这取决于所使用的文件系统。例如,ext4 的最大文件名长度为 256 字节,路径名长度不受限制。
See Comparison of file systemsfor more.
回答by ncmathsadist
This is not bash-dependent; it's OS dependent. On a mac, its 0xff for a filename and 0x400 or so for a path name. Ubuntu 9 had a limit of 144 characters for file names.
这不依赖于 bash;它依赖于操作系统。在 Mac 上,它的 0xff 表示文件名,0x400 左右表示路径名。Ubuntu 9 的文件名限制为 144 个字符。
I have found this link in Wikipedia. It tells path and filename limits for numerous file systems.
我在维基百科中找到了这个链接。它告诉众多文件系统的路径和文件名限制。
回答by Michael Aaron Safyan
The Single UNIX Specification mentions NAME_MAX
and PATH_MAX
constants in limits.hthat can be read with pathconf. However, this is very filesystem dependent, and you are unlikely to hit such a limit.
单一 UNIX 规范提到NAME_MAX
和可以使用pathconf读取的limits.h中的PATH_MAX
常量。但是,这非常依赖于文件系统,您不太可能达到这样的限制。
NOTE: As a programmer, you should not hard-code these limits. You should use dynamic allocation, so that it will always work so long as the underlying system allows for whatever you are doing.
注意:作为程序员,您不应硬编码这些限制。你应该使用动态分配,这样只要底层系统允许你在做什么,它就会一直工作。
回答by dogbane
In a temp directory, run:
在临时目录中,运行:
num=1
while [ true ]
do
if ! touch $(printf "%${num}s" | tr ' ' 'a')
then
echo $num
break
fi
((num++))
done
and I get:
我得到:
touch: cannot touch `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa': File name too long
256
which means my limit is 255.
这意味着我的限制是 255。
回答by tim
On Mac OS X 10.6.7:
在 Mac OS X 10.6.7 上:
man getconf
getconf NAME_MAX / # 255 bytes
getconf PATH_MAX / # 1024 bytes
# check file path length with wc before using touch, mkdir, etc.
echo '/very/lllooooonnnnnggggg/file/path.txt' | wc -c
回答by David Bala?ic
- Is there a limit on the number of characters in the absolute path name of a file?
- 文件的绝对路径名中的字符数有限制吗?
Yes, there is.
就在这里。
See answer by sfpat the question Filename length limits on linux?on serverfault
请参阅sfp在Linux 上的文件名长度限制问题中的回答?服务器故障
In short:
简而言之:
#define PATH_MAX 4096 /* # chars in a path name including nul */
And for:
对于:
- Is there a limit on the number of characters for the filename (without extension) only?
- 文件名(无扩展名)的字符数是否有限制?
in the same linked answer:
在同一个链接答案中:
#define NAME_MAX 255 /* # chars in a file name */
回答by olibre
I refer to other answers, please upvote them.
我参考了其他答案,请点赞。
On Linux, filename and pathname lengths depends on :
在 Linux 上,文件名和路径名长度取决于:
- file-system limitsas stated by eugene yand ncmathsadist;
- constant defined in
linux/limits.h
before compilation as stated by Michael Aaron Safyanand later David Bala?ichas pointed to a similar question.
- eugene y和ncmathsadist所述的文件系统限制;
linux/limits.h
正如Michael Aaron Safyan和后来的David Bala?ic所说,在编译之前定义的常量指出了一个类似的问题。
To dynamically get these properties in bash:
要在bash 中动态获取这些属性:
回答by jp.rider63
FYI, on Docker, the filename limit is currently 242 characters.
仅供参考,在 Docker 上,文件名限制目前为242 个字符。