Limit on file name length in bash

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?

111533 次浏览

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.

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.

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.

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
  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 */

I refer to other answers, please upvote them.

On Linux, filename and pathname lengths depends on :

To dynamically get these properties in :

  • 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
    

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