What is the maximum file name length in linux and how to see this ? Is this really 255 characters ?

Usually we are aware that maximum length for a filename under Linux is 255 bytes.

And everyone assumes that 255 bytes means we can have a filename upto 255 characters.

However this is not true, we will see exactly what is the limit of file name in linux and how to see that.

1. Check File name maximum length limit

[root@ngelinux01 ~]# getconf -a | grep -i name_max
NAME_MAX                           255
_POSIX_NAME_MAX                    255
LOGNAME_MAX                        256
TTY_NAME_MAX                       32
TZNAME_MAX                         6
_POSIX_TZNAME_MAX                  6
CHARCLASS_NAME_MAX                 2048
HOST_NAME_MAX                      64
LOGIN_NAME_MAX                     256
[root@ngelinux01 ~]# getconf -a | grep -i path_max
PATH_MAX                           4096
_POSIX_PATH_MAX                    4096
[root@ngelinux01 ~]#

Here we can see the maximum length of file name is 255 bytes.

2. Lets create a file of 256 characters.

### a. Creating a 254 character filename.
[root@host-1-89 ~]# echo > a.txt; i=1; while [ $i -lt 255 ] ; do echo $i >> a.txt; (( i = $i + 1 )) ; done
[root@host-1-89 ~]# wc -l a.txt
254 a.txt
[root@host-1-89 ~]# cat a.txt | tr -d "\n"
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256[root@host-1-89 ~]#

### b. Try to create the file of 255 characters, half 120 characters.
[root@host-1-89 ~]# touch 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661
touch: cannot touch ‘1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661’: File name too long

### c. Getting error in both cases, try to short the limit below 65 character and it will be created.
[root@ngelinux01 ~]# touch 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091

 

3. Checkout the newly created file.

[root@ngelinux01 ~]# ls -ltr
total 32
-rw-------. 1 root root  1547 Sep  8  2014 anaconda-ks.cfg
-rw-r--r--. 1 root root   917 Jul 12 15:16 a.txt
-rw-r--r--. 1 root root     0 Jul 12 15:17 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565
-rw-r--r--. 1 root root     0 Jul 12 15:17 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091

 

4. Understanding why we are not able to create the file with enough characters.


Linux OS has a 255 bytes limit of a file name, not exactly 255 characters.

There is a major difference between character and bytes in file naming.

Say for example, if we use UTF-8 encoding, we may end up with filenames of very less limit of 63 characters at maximum.

Usually the characters in UTF encoding takes upto 4 bytes per character, hence the limit will be 255/4 = 63 characters.

Leave a Reply

Your email address will not be published.