85

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?

Sriram
  • 10,298
  • 21
  • 83
  • 136
  • 6
    I always wondered how people could hit limits like filename length. what are you doing? – Karoly Horvath Jul 04 '11 at 12:26
  • @yi_H: LOL! nothing, just writing a big file name (absolute path) to a file and reading the name from it. The reading takes place within asterisk, and I must figure out how many characters I need to read. This question sprang as an aside from there. – Sriram Jul 04 '11 at 12:30
  • 1
    This smells so much like homework...but in the end it belongs on ServerFault, SuperUser or Unix&Linux. – Bobby Jul 04 '11 at 12:31
  • 1
    @Bobby: If it were homework, I would have labelled it so. – Sriram Jul 04 '11 at 12:31
  • @Sriram: Yes, I thought so. That was just a conclusion, no insult intended. – Bobby Jul 04 '11 at 12:33
  • 1
    @Bobby: not to worry, i did not take your comment as an insult or anything of that sort.. cheers! – Sriram Jul 04 '11 at 12:38
  • 1
    @KarolyHorvath I was only trying in my code to code information on how data were generated into the filename, but failed to write `im_syn_profile_BT,Radar94Z,_to_IWC,_chans1,2,3,4,5,6,7,8,9,10,11,12,_noise1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1.000000e-01,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,_0.mat` :( – gerrit Mar 20 '13 at 15:56
  • Bash, of course, simply inherits whatever limit the OS it runs on imposes. – tripleee Sep 22 '21 at 09:14
  • @KarolyHorvath Well, inside an ecryptfs-mounted home folder I am hitting a name limit (`getconf NAME_MAX $HOME`) of 143. I'm hitting the limit all the time. – 0xC0000022L Nov 25 '21 at 21:08

9 Answers9

81

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.

Chris Noe
  • 36,411
  • 22
  • 71
  • 92
Bite code
  • 578,959
  • 113
  • 301
  • 329
  • 7
    There is also the linux kernel imposed limit [PATH_MAX](http://stackoverflow.com/questions/9449241/where-is-path-max-defined-in-linux) defined in linux/limits.h, which is usually 4096 char's. The last character is reserved for the string terminator null character. – BeowulfNode42 Feb 19 '14 at 23:46
  • Also each application may have its own limit that it imposes. – BeowulfNode42 Feb 19 '14 at 23:47
  • In case someone has any doubts: combining this response to [this one](http://stackoverflow.com/a/6571693/982542) on a `ecryptfs`, I have been able to verify the bug is still unresolved (though reading the bug itself make me think it will never be resolved) – ThanksForAllTheFish Mar 09 '15 at 15:52
43

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 1
    I get 144, which means my limit is 143. Why so short!? I'm running Ubuntu 16.04 LTS 64-bit. – Gabriel Staples Jun 13 '19 at 06:29
  • 1
    @GabrielStaples you are probably using eCryptFS. Btw, this answer is very useful. Thanks! – acdcjunior Oct 10 '21 at 17:51
  • `while [ true ]` is a common antipattern; it works, but not for the reason you probably think. `while [ false ]` would work equally well; you are testing whether the string between `[` and `]` is not empty, which of course it isn't. The proper idiomatic way to write an endless loop is simply `while true` – tripleee May 24 '23 at 07:34
17

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
tim
  • 171
  • 2
  • For what it's worth, `getconf` is in [POSIX](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/getconf.html) so definitely not MacOS only. – tripleee Sep 22 '21 at 09:16
  • Actually, the usage is identical; the code in this answer checks the limit for the root file system. – tripleee May 24 '23 at 07:36
14

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
    
Community
  • 1
  • 1
oHo
  • 51,447
  • 27
  • 165
  • 200
9

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.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
4
  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 */
Community
  • 1
  • 1
David Balažic
  • 1,319
  • 1
  • 23
  • 50
  • 1
    Thanks for the link to the similar question on serverfault. But I do not think the answer from sft is pretty good. I prefer the answer from @WerkkreW giving a good link: http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits – oHo May 19 '13 at 21:42
  • 1
    That link is about file systems, not bash or linux as stated in the question. For example it lists no limit defined for ext3 file system, but linux has a limit of 4096 (regardless of filesystem). – David Balažic May 20 '14 at 09:58
  • 1
    I agree with you :-) Thank you for pointing to a similar question +1 :-) Let's say it depends on both : FS and `linux/limits.h`. But the ultimate answer would be something like `cat /proc/sys/fs/file-max` or... `getconf NAME_MAX /` ... Yes last command is the good one ... and [tim](http://stackoverflow.com/a/6582758/938111) as found it before me. Cheers – oHo May 24 '14 at 06:47
4

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

jp.rider63
  • 574
  • 3
  • 20
3

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.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
2

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.

ncmathsadist
  • 4,684
  • 3
  • 29
  • 45