Limit on file name length in bash [closed]

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.


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

I refer to other answers, please upvote them.

On Linux, filename and pathname lengths depends on :

  • file-system limits as stated by eugene y and ncmathsadist ;
  • constant defined in linux/limits.h before compilation as stated by Michael Aaron Safyan and later David Balažic has pointed to a similar question.

To dynamically get these properties in 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