Limit on file name length in bash

LinuxBashFileMaxlength

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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.

Solution 3 - Linux

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

Solution 4 - Linux

I refer to other answers, please upvote them.

On Linux, filename and pathname lengths depends on :

To dynamically get these properties in [tag:bash]:

  • 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 5 - Linux

The Single UNIX Specification mentions NAME_MAX and PATH_MAX constants in http://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html">limits.h</a> that can be read with http://pubs.opengroup.org/onlinepubs/009695399/functions/pathconf.html">pathconf</a>;. 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.

Solution 6 - Linux

> 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: > 2. 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 */

Solution 7 - Linux

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

Solution 8 - Linux

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.

Solution 9 - Linux

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSriramView Question on Stackoverflow
Solution 1 - Linuxe-satisView Answer on Stackoverflow
Solution 2 - LinuxdogbaneView Answer on Stackoverflow
Solution 3 - LinuxtimView Answer on Stackoverflow
Solution 4 - LinuxoHoView Answer on Stackoverflow
Solution 5 - LinuxMichael Aaron SafyanView Answer on Stackoverflow
Solution 6 - LinuxDavid BalažicView Answer on Stackoverflow
Solution 7 - Linuxjp.rider63View Answer on Stackoverflow
Solution 8 - LinuxEugene YarmashView Answer on Stackoverflow
Solution 9 - LinuxncmathsadistView Answer on Stackoverflow