Limit file name length in bash

The following questions are for bash only and for Linux only:

  • Is there a limit on the number of characters in the absolute path of a file name?
  • Is there a character limit for the file name (without extension)?

If so, what could be these limitations? How can I access them if they are systemic?

+59
linux file bash maxlength
Jul 04 2018-11-11T00:
source share
9 answers

It is very dependent on the file system. For ext FS (currently most commonly used on Linux):

  • max file name length: 255 bytes
  • maximum path length: none

The extension is not something FS knows, it contains 255 bytes, the extension is added (you can have file names without any extensions).

Here is a more comprehensive list of these limits, for FS.

There may also be extensions on the file system that can also change the maximum length. For example, eCryptFS, which uses part of a lower file name to store metadata and limits the file name to a maximum length of 143 characters. See the launch tab of Ubuntu eCryptFS .

+53
Jul 04 2018-11-12T00:
source share

In the 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 : File name too long touch: cannot touch `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa': File name too long 256 

which means my limit is 255.

+32
Jul 04 '11 at 12:45
source share

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 
+14
Jul 05 2018-11-12T00:
source share

I refer to other answers, please promote them.

On Linux, the file name and path length depend on:

  • file system restrictions as indicated by eugene y and ncmathsadist ;
  • a constant defined in linux/limits.h before compilation, as pointed out by Michael Aaron Safyan , and later David Balazic pointed out a similar question .

To dynamically get these properties in bash :

  • Create a file name (or path) longer and longer as described by dogbane
  • Use the getconf command suggested by tim , which is also available on Linux:

     $ getconf NAME_MAX /mnt/sda2/ 255 $ getconf PATH_MAX /mnt/sda3/ 4096 
+9
May 24 '14 at 7:20
source share

One UNIX specification mentions the constants NAME_MAX and PATH_MAX in limits.h , which can be read using pathconf . However, it is very dependent on the file system, and you are unlikely to reach that limit.

NOTE. As a programmer, you should not hard code these restrictions. You must use dynamic allocation so that it always works as long as the underlying system allows everything you do.

+6
Jul 04 '11 at 12:35
source share
  • Is there a limit on the number of characters in the absolute path of a file name?

Yes there is.

See sfp answer in linux file name length restrictions question ? server error

In short:

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

And for:

  1. Is there a character limit for the file name (without extension)?

in the same related answer:

 #define NAME_MAX 255 /* # chars in a file name */ 
+3
May 19 '13 at 16:46
source share

It depends on the file system used. For example, ext4 has a maximum file length of 256 bytes and an unlimited path name length.

See Comparing file systems for more details.

+2
Jul 04 2018-11-12T00:
source share

It is not bash-dependent; it depends on the OS. On a mac, its 0xff for the file name and 0x400 or so for the path name. Ubuntu 9 had a limit of 144 characters for file names.

I found this link on Wikipedia . It reports the path and file name restrictions for numerous file systems.

+1
Jul 04 '11 at 12:32
source share

FYI, on Docker, the file name limit is currently 242 characters .

0
Mar 16 '17 at 17:08
source share



All Articles