开发者

Limit on file name length in bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.开发者_如何学编程

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

Closed 1 year ago.

The community reviewed whether to reopen this question 1 year ago and left it closed:

Original close reason(s) were not resolved

Improve this question

The following questions are meant for bash and linux only:

  1. Is there a limit on the number of characters in the absolute path name of a file?
  2. 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?


It depends very much on the filesystem. For the ext FS (currently the most used on Linux):

  • max filename length: 255 bytes
  • 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).

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.


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.


On 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


I refer to other answers, please upvote them.

On Linux, filename and pathname lengths depends on :

  • file-system limits as stated by eugene y and 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.

To dynamically get these properties in bash:

  • Create a filename (or pathname) longer and longer as explained by dogbane
  • Use the command getconf as proposed by tim that is also available on Linux:

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


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.

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.


  1. Is there a limit on the number of characters in the absolute path name of a file?

Yes, there is.

See answer by sfp at the question Filename length limits on linux? on 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?

in the same linked answer:

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


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


It depends on the filesystem used. For example, ext4 has maximum filename length of 256 bytes and unlimited pathname length.

See Comparison of file systems for more.


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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜