Filename length limits on linux?

239,688

Solution 1

See the Wikipedia page about file systems comparison, especially in column Maximum filename length.

Here are some filename length limits in popular file systems:

BTRFS   255 bytes
exFAT   255 UTF-16 characters
ext2    255 bytes
ext3    255 bytes
ext3cow 255 bytes
ext4    255 bytes
FAT32   8.3 (255 UCS-2 code units with VFAT LFNs)
NTFS    255 characters
XFS     255 bytes

Solution 2

I've read here that path length limit is in system headers. File name length limit is there too. On my system it's file:

  /usr/src/linux-headers-2.6.38-10/include/linux/limits.h

and C-lang defines:

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

and some more.

Solution 3

I refer to other answers, please upvote them.

Are there any filename or path length limits on Linux?

Yes, filename and pathname lengths are limited by :

To dynamically get these properties:

  • Use functions pathconf and fpathconf as proposed by Michael Aaron Safyan
  • 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
    

Solution 4

And for the sake of saving time (and anchoring it to memory):

ext2, ext3, ext4, zfs: no pathname limits; 255 bytes filename limit.

Solution 5

Those are file system name lengths. "linux" itself has some too. For instance, from bits/stdio_lim.h:

# define FILENAME_MAX 4096
Share:
239,688

Related videos on Youtube

Ripei
Author by

Ripei

Updated on September 17, 2022

Comments

  • Ripei
    Ripei over 1 year

    Are there any filename or path length limits on Linux?

  • Ivan
    Ivan almost 15 years
    So since the extX filesystems have a lower filename limit than what's defined in the kernel, you wouldn't ever hit that limit, unless it also encompases pathnames, right?
  • jj33
    jj33 almost 15 years
    that's what it looks like to me. There's also PATH_MAX for the path, which is 4096, so it would be hit before the "unlimited" path size on the exts... I'm not sure how the OS resolves its own internal restrictions and those of the FS, never had my arms in that deep. interesting question though.
  • Avery Payne
    Avery Payne almost 15 years
    4096 characters is a helluva path name. I'm sure it could be raised with a recompile, but honestly, /why would you need pathnames that long?/
  • jj33
    jj33 almost 15 years
    I'm not sure you would need it or not. I view it more as a protection against malicious or negligent programs (I could easily see a script that behaves poorly and begins creating the same dir recursively. Actually, I've made that script, but it was redirecting a web site, not creating dirs...).
  • Hubert Kario
    Hubert Kario almost 12 years
    @AveryPayne To add tags to files so they could be searched using a simple locate.
  • Avery Payne
    Avery Payne almost 12 years
    @HubertKario just curious, why not enable user_xattr, then store your tags as comments, retrieving files by doing lsattr -R | grep 'mytag'? I guess you could stuff the tags into the file name but it seems like you would just clutter up your name space with absurdly long names.
  • Hubert Kario
    Hubert Kario almost 12 years
    @AveryPayne Your solution would be ideal, if there was any way to index or search extended attributes. From what I know only BeOS had indexes over xattrs and none of Linux FS do...
  • doc_id
    doc_id over 11 years
    answer is: limit is usually 255 chars (for those who are too lazy to click this link)
  • nonchip
    nonchip almost 10 years
    @rahmanisback that's right for filename limits, while path limits are usually defined by the OS, not FS (except for some strange FSes like iso or ntfs), and, on linux, are 4K
  • doc_id
    doc_id almost 10 years
    @nonchip thanks for clarification although the question was about filename length not paths.
  • nonchip
    nonchip almost 10 years
    Actually it was about both :D
  • Thomas Weller
    Thomas Weller about 9 years
    255 includes the extension. It's not like 8+3 in DOS.
  • Kris
    Kris almost 9 years
    Just pointing out: bytes != chars, especially if you use UTF-8. See here.
  • Kris
    Kris almost 9 years
    Just pointing out: bytes != chars, especially if you use UTF-8. See here.
  • tripleee
    tripleee about 8 years
    A simple portable way to find the maximum length empirically is to write a program which creates longer and longer directory chains, and see where it fails. You won't know exactly why it fails (though you would hope for a suggestive human-readable error message) but you'll know how far you can safely go. Remember to check for both individual directory length, relative pathname length, and absolute pathname length.
  • tripleee
    tripleee about 8 years
    Also, e.g. the Python os.pathconf() module will have some answers; if the Python port is any good, they should be reasonable.
  • Björn Lindqvist
    Björn Lindqvist about 8 years
    You can't because some filesystems don't impose any limits. It would sooner fail with an out of memory error which any program would have a hard time recovering from.
  • Rahly
    Rahly almost 8 years
    bytes == chars, but bytes != characters. Using UTF-8 the proper terminology would be bytes == Coding Unit, bytes != Code Point. FYI, the max characters for UTF-8 for 255, is 63-255 Code Points. A C/C++ char is 8 bits or 1 byte.
  • Rahly
    Rahly almost 8 years
    @DavidBalažic: Although true, PATH_MAX under linux is just a guideline, most of the underlying file systems do not have a limitation. This makes it hard to reference paths that greater than that size. I usually use "chunks" of PATH_MAX as a size.
  • Rahly
    Rahly almost 8 years
    This is the correct answer, except this is due to @BjörnLindqvist comment. PATH_MAX is just a guideline, and 99% of files will probably be within that limit.
  • thoredge
    thoredge almost 7 years
    Also worth noting. In docker the maximum path length is 242
  • jlh
    jlh over 6 years
    I'm shocked by these limits. Files per directory is unlimited, 4 billion files per volume, file sizes into the terabytes, volume sizes go to exabytes but we have a stupid limit of 255 bytes for file names?
  • Mikko Rantalainen
    Mikko Rantalainen about 6 years
    Most programs are limited with absolute paths to PATH_MAX = 4096, though. That can be worked around if your program is able to use relative paths and you change your working directory first.
  • Mikko Rantalainen
    Mikko Rantalainen about 6 years
    @HubertKario it would be pretty trivial to teach locate about extended attributes. For searching, the thing you put to the index does not need to be only the original filename.
  • Hubert Kario
    Hubert Kario about 6 years
    @MikkoRantalainen other problem is that xattrs are very error prone for handling (copying, moving, backing up). And most likely you wouldn't like locate to index all attributes, so then you need to handle non-standard configurations...
  • ntninja
    ntninja about 6 years
    It should also be mentioned that if you layer eCryptFS with filename encryption on top of these file systems (as installed in Ubuntu with encrypted home dir option) the effective maximum filename length will only be 143 characters. See: unix.stackexchange.com/a/32834/47938
  • Ciro Santilli Путлер Капут 六四事
    Ciro Santilli Путлер Капут 六四事 over 5 years
    Here is a pathconf example in C: stackoverflow.com/questions/16285623/…
  • ssokolow
    ssokolow about 5 years
    It's because various POSIX APIs such as getcwd and realpath (which you can reimplement in userspace code by reading the metadata for . and then changing to .. and repeating until you hit the filesystem root) rely on PATH_MAX. (Source)
  • Dan Dascalescu
    Dan Dascalescu over 4 years
    This 255-bytes limit is actually pretty silly. There are very practical use cases for longer filenames, e.g. long course or book or paper titles along with by the list of author names. And there's software that breaks when it can't write the complete filename, e.g. youtube-dl when downloading the video of such a course.
  • Mikko Rantalainen
    Mikko Rantalainen over 3 years
    Yes, however, awfully lot of misc library code is prone to break if your code changes current directory at will. A well made code doesn't break but most of the existing code is not well made.
  • Melab
    Melab about 3 years
    @Rahly Where do you use "chunks"? In a program? In a filesystem driver?
  • Rahly
    Rahly about 3 years
    either, but i'm talking about a program