8

Why am I getting No space left on device when I try to create more than 32767 files in my vFat USB Flash Drive?

Info

  • Flash drive is empty (freshly formatted)
$ df -H
> Filesystem      Size  Used Avail Use% Mounted on
> /dev/sdb1        16G  2.1M   16G   1% /media/cmhteixeira/cmhteixeira-usb
  • gnome disk shows:
Partition Type: W95 FAT32 (LBA)
Contents: FAT (32-bit version)

I keep reading online that FAT32/vFat have some limitations, including:

  • Maximum of 65,534 files in a single directory. Less if the file names are not (8.3).

I am creating these files in a single directory( not necessarily root directory), with the format 12345678.txt. But after creating the first 32767 such files, I get no space left on device error. It is suspicious that the maximum number of files is exactly half the maximum I read on the internet (2^16-2=65,534 vs 2^15-1=32,767).

Do you know why?

(I can create more files if I create them within separate directories)

5
  • 3
    In a nutshell, use exFAT instead. Nov 16, 2023 at 22:16
  • 5
    Thanks. I am more interested in trying to understand the reason, rather than looking for a solution. Can you elaborate on your suggestion? Nov 16, 2023 at 22:24
  • 1
    FAT32 is very old and, in most aspects, really outdated. In your question you already mentioned some of its limitations. Nov 16, 2023 at 22:31
  • 2
    @Hearth : I suppose, minus two for . and .. - so 65534 user-made entries (as the useful theoretical max).
    – Jim Klimov
    Nov 18, 2023 at 13:20
  • Is there a reason for so many files? tar or zip them instead ?
    – Criggie
    Nov 19, 2023 at 8:48

3 Answers 3

22

FAT32 32-bit short file names must also be uppercase (technically any ASCII character in the range below 0x80, excluding space), to hit the maximum of 65,534 files.

Long file names will take up more 32-bit entries, so all of your files are taking 2 chunks resulting in half the max files

1
  • 5
    This is because FAT long filenames are stored using "hidden" directory entries adjacent to the "real" directory entry, with the Unicode filename stored in the fields that would otherwise be used for the name, attributes, mtime, etc. Each LFN can store up to 13 characters, and the filename length limit is 255 characters, so a single file can occupy up to 21 directory entries (20 LFN entries plus one "real" one). In this case the names are all 11 chars, so every file takes up two entries.
    – hobbs
    Nov 17, 2023 at 17:15
12

You already have the answer

I keep reading online that FAT32/vFat have some limitations, including:

Maximum of 65,534 files in a single directory. Less if the file names are not (8.3).

You are creating a filename with extension txt. For 8.3 format, the filenames have to be all capitals. Try using .TXT instead.

5

As mentioned in the other answers, a second directory entry is created with the long filename, since "txt" is lowercase (instead of uppercase) and thus a LFN entry is generated to store that.

However, since it's only a case difference in the extension, it is possible to avoid the LFN by saving this on 0x0C byte, which can be enabled on Linux with the mount option shortname:

If a filename contains only lowercase letters, or is a combination of a lowercase basename with an uppercase extension, or vice versa; and has no special characters, and fits within the 8.3 limits, a VFAT entry is not created on Windows NT and later versions of Windows such as XP. Instead, two bits in byte 0x0C of the directory entry are used to indicate that the filename should be considered as entirely or partially lowercase. Specifically, bit 4 means lowercase extension and bit 3 lowercase basename, which allows for combinations such as "example.TXT" or "HELLO.txt" but not "Mixed.txt". Few other operating systems support it. This creates a backwards-compatibility problem with older Windows versions (Windows 95 / 98 / 98 SE / ME) that see all-uppercase filenames if this extension has been used, and therefore can change the name of a file when it is transported between operating systems, such as on a USB flash drive. Current 2.6.x versions of Linux will recognize this extension when reading (source: kernel 2.6.18 /fs/fat/dir.c and fs/vfat/namei.c); the mount option shortname determines whether this feature is used when writing.

https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system#VFAT_long_file_names

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .