热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在bash中限制文件名长度-Limitonfilenamelengthinbash

Thefollowingquestionsaremeantforbashandlinuxonly:以下问题仅适用于bash和linux:Istherealimit

The following questions are meant for bash and linux only:

以下问题仅适用于bash和linux:

  1. Is there a limit on the number of characters in the absolute path name of a file?
  2. 文件的绝对路径名中的字符数有限制吗?
  3. Is there a limit on the number of characters for the filename (without extension) only?
  4. 文件名的字符数是否有限制(没有扩展名)?

If so, what might these limits be? How can I access them in case they are system specific?

如果是这样,这些限制是什么?如果它们是特定于系统的,我如何访问它们?

9 个解决方案

#1


56  

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
  • 最大文件名长度:255字节
  • max path length: none
  • 最大路径长度:没有

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个字节,扩展名(您可以没有任何扩展名的文件名称)。

Here is a more exhaustive list of these limits, per 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 launchpad entry.

也可以对文件系统进行扩展,从而更改最大长度。例如,eCryptFS使用部分较低的文件名来保存元数据,并将文件名限制为143个字符的最大长度。参见Ubuntu eCryptFS启动条目。

#2


33  

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。

#3


14  

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

#4


10  

I refer to other answers, please upvote them.

我参考其他答案,请投赞成票。

On Linux, filename and pathname lengths depends on :

在Linux上,文件名和路径名的长度取决于:

  • file-system limits as stated by eugene y and ncmathsadist ;
  • 由eugene y和ncmathsadist规定的文件系统限制;
  • constant defined in linux/limits.h before compilation as stated by Michael Aaron Safyan and later David Balažic has pointed to a similar question.
  • 常量定义在linux /限制。h在编译之前如上所述由迈克尔·亚伦Safyan后来大卫Balažic指着一个类似的问题。

To dynamically get these properties in bash:

在bash中动态获取这些属性:

  • Create a filename (or pathname) longer and longer as explained by dogbane
  • 创建一个文件名(或路径名)的时间越来越长,就像dogbane所解释的那样
  • Use the command getconf as proposed by tim that is also available on Linux:

    使用tim建议的getconf命令,该命令在Linux上也可用:

    $ getconf NAME_MAX /mnt/sda2/
    255
    $ getconf PATH_MAX /mnt/sda3/
    4096
    

#5


7  

The Single UNIX Specification mentions NAME_MAX and PATH_MAX constants in limits.h that can be read with pathconf. However, this is very filesystem dependent, and you are unlikely to hit such a limit.

单一的UNIX规范在限制中提到NAME_MAX和PATH_MAX常量。可以用pathconf读取的h。然而,这是非常依赖于文件系统的,您不太可能达到这样的限制。

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.

注意:作为程序员,不应该硬编码这些限制。您应该使用动态分配,以便只要底层系统允许您所做的任何事情,它将始终有效。

#6


3  

  1. Is there a limit on the number of characters in the absolute path name of a file?
  2. 文件的绝对路径名中的字符数有限制吗?

Yes, there is.

是的,有。

See answer by sfp at the question Filename length limits on linux? on serverfault

请参见sfp在linux上的问题文件名长度限制上的答案?在serverfault

In short:

简而言之:

#define PATH_MAX        4096    /* # chars in a path name including nul */

And for:

和:

  1. Is there a limit on the number of characters for the filename (without extension) only?
  2. 文件名的字符数是否有限制(没有扩展名)?

in the same linked answer:

在同一个链接的答案中:

#define NAME_MAX         255    /* # chars in a file name */

#7


2  

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 systems for more.

有关更多信息,请参见文件系统的比较。

#8


1  

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-dependent;它是依赖于操作系统的。在mac上,文件名是0xff,路径名是0x400。Ubuntu 9的文件名限制为144个字符。

I have found this link in Wikipedia. It tells path and filename limits for numerous file systems.

我在维基百科上找到了这个链接。它告诉许多文件系统的路径和文件名限制。

#9


0  

FYI, on Docker, the filename limit is currently 242 characters.

FYI,在Docker上,文件名限制目前为242个字符。


推荐阅读
author-avatar
小圈44
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有