Limit on File Name Length in Bash

1. Overview

In Unix-based systems, there is a limit on the length of filenames. This limit is different on different file systems.

In this tutorial, we’ll learn how to find the limit on different file systems. Additionally, we’ll create a file with a long name to confirm how the limit works.

2. File Name Length Limits and Unix File Systems

Most Unix file systems have similar filename length limits:

File SystemMax File Name Length
BTRFS255 bytes
exFAT255 UTF-16 characters
ext2255 bytes
ext3255 bytes
ext3cow255 bytes
ext4255 bytes
FAT328.3 (255 UCS-2 code units with VFAT LFNs)
NTFS255 characters

However, since the Unicode representation of a character can occupy several bytes, the maximum number of characters that comprise a path and filename can vary.

Further, the limits remain the same whether the filename contains an extension or not. We can take a look at a more exhaustive list of the filename and path limits depending on the file system.

We can also have extensions on our file system that change the maximum length limit. A good example of this is eCryptFS which utilizes part of the lower filename to keep metadata and sets the limit of a filename to 143 characters.

3. Finding the Limit

Linux has a handy command called getconf for querying system configuration variables.

We can run it to find the filename length limit:

$ getconf -a | grep -i name_max
NAME_MAX                           255
_POSIX_NAME_MAX                    255
LOGNAME_MAX                        256
TTY_NAME_MAX                       32
TZNAME_MAX                         
_POSIX_TZNAME_MAX                  
CHARCLASS_NAME_MAX                 2048
HOST_NAME_MAX                      64
LOGIN_NAME_MAX                     256
Copy

Here, the NAME_MAX configuration represents the filename length limit.

Similarly, we can find the path length limit by modifying the last part of our previous command:

$ getconf -a | grep -i path_max
PATH_MAX                           4096
_POSIX_PATH_MAX                    4096Copy

4. Confirming the Limits

Let’s use the standard touch command to try and create a file with a name containing 258 characters, i.e., three characters more than the limit of 255:

$ touch abcabcabcabcabcabcabc...other characters omitted....abcabcabc
Copy

Here’s the response we get:

touch: cannot touch 'abcabcabcabcabcabcabc...other characters omitted....abcabcabc': File name too longCopy

5. Conclusion

In this article, we learned how to check both filename and path limits on different Linux file systems. We also tried to create a file that surpassed the limit to see the error we got.

Comments are closed on this article!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值