Does OS X enforce a maximum filename length or character restriction?

What is the maximum filename character length of files in OS X?

Of secondary importance is whether some characters are allowed/disallowed in addition to any overall length restrictions.


Solution 1:

From the font of all knowledge, the maximum file name size in the default HFS+ file system is 255 UTF-16 encoding units, so basically 255 characters. Technically, any unicode character can be used, but this may be practically limited by the application you are using - for example, the / character is used to delineate directories in pathnames, so you may not be allowed to name a single file stuff/things.txt, as this means "the file things.txt in the stuff directory" to the operating system. There are ways of "escaping" characters like / in filenames, but for general use it's discouraged.

Solution 2:

Yes - you should keep file names to 255 characters but paths can be longer but not as long as some other unix allows.

The desktop OS X supports ten major filesystems (HFS, HFS Plus, UFS, WebDAV, UDF, FAT, SMB/CIFS, AFP, NFS, and FTP) with several variants of some of these. See table 1 at developer.apple.com's article below for details:

  • Overview of OS X File Systems

The system default filesystem, HFS plus supports filenames of 255 UTF-16 encoding units and the only reserved character is : which is used internally to denote a directory change as opposed to / which normally is reserved to signify a directory change rather than belonging to the name of the file. (ok - I believe 0x00, the null character also is forbidden in HFS+)

By convention, software should use file handles and are responsible for allocating enough space to contain an arbitrarily long file path if it doesn't use standard API for navigating the filesystem structure to locate a 255 length filename.