13

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.

1
  • 1
    Assuming the HFS+ file system?
    – vcsjones
    Commented Mar 24, 2013 at 20:33

2 Answers 2

14

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.

6
  • 3
    That with the / is not true (10.6 here). What you can't use is : (old HFS separator), at least in the Finder. Funny thing is that / is represented as : at the shell level. Another strange limitation is that the path length is limited by 1024 bytes or UTF-8 chars, not sure which. This isn't enforced by Finder and can lead to strange behaviour. Commented Mar 24, 2013 at 21:11
  • @PercivalUlysses You are correct : is forbidden on HFS and HFS+ but most UNIX folk are used to automagic substitution of / for : and vice versa and don't realize HFS's internal storage uses : to separate directories from file names.
    – bmike
    Commented Mar 24, 2013 at 21:13
  • 2
    @PercivalUlysses Yes - on Mountain Lion I was able to make a directory path 1037 characters deep and plop a file and spotlight, finder aren't happy to navigate to, locate or open that file in Text Edit until I move it up one folder (where the path is 996 long) and everything works as expected. Also pathchk -p is handy for running these sort of portability checks.
    – bmike
    Commented Mar 24, 2013 at 21:38
  • 5
    I tested the following: touch 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345 works fine. But touch 12345678901234567890....6 with one more character throws: File name too long. FYI touch just creates an empty file, or actually sets the existing file's time_modified to now.
    – AlexGrafe
    Commented Jan 5, 2018 at 17:27
  • 2
    @AlexGrafe I managed to make 8 nested directories with that path name. PATH_MAX is only encountered when giving absolute paths to system calls, when you give relative directories the full path length seems to be unlimited. Big Sur, APFS. Commented Mar 8, 2021 at 9:37
5

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:

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.

3
  • touch : and mkdir : work though Commented Sep 5, 2018 at 12:25
  • @StefanDragnev Did you look at them how they show up in Finder? There's an automagic substitution (: is the path separator, but the Unix side switches : and / in all paths) Commented Mar 8, 2021 at 9:38
  • Remarkable. Finder shows a file/folder named /. Xcode also shows the file name as /. VS Code and ls both show the name as :. Commented Mar 8, 2021 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .