Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the length of a filename affect remaining storage space on a disk?

How does the length of a filename affect remaining storage space on a disk?

I realize this is filesystem dependent. In particular I am thinking about the EXT series of file systems. I don't fully understand how inodes affect disk space and how the filename itself is stored. It's difficult to get relevant search results for this question too. That's why I'm asking here. On linux, the maximum file name length is usually 255 or 256 characters. When the file system is created, is that amount of space "reserved" for each and every file name? In other words, is disk storage not affected by the actual file name because the maximum is already used? Or is it more complicated than that?

Suppose, I have a file named "joe.txt" and rename it to "joe2.txt". Has the amount of available disk space decreased after this? What about longer names like "joe_version.txt" or "joe_original_version_with_bug_that_Jim_solved.txt"? I am worried about thresholds at 8, 16, 32, 64, etc characters. I will be storing millions of images. I have never bothered to worry about such an issue before so I'm not completely sure how this works.

Although EXT is the only filesystem I'm using, discussing FAT and others might be useful to somebody else that has a similar question.

like image 374
SO Stinks Avatar asked Sep 23 '10 02:09

SO Stinks


People also ask

Do longer file names take up more space?

The length of the file name has nothing to do with how much space it takes on the disk.

Does file name affect size?

No, they don't. File names are metadata, and do not appear in the file itself.

Do long file names cause problems?

If a file's full path length is too long, you may not be able to access the file and a server may not be able to back it up or even find it.

Why is the size of the file and the size on disk different numbers?

The size on the disk is the indicator of how much space a file occupies on the hard disk drive. When a file is stored in a modern storage device, it usually consumes slightly more storage than the actual file. That's the reason why you see the difference between size vs size on disk.


1 Answers

On Linux (or more generally, Unix type filesystems) file names are stored in directory entry inodes, which contain a list of (filename, inode number) mappings for each file in the directory. My understanding is that for each filename there is reserved space for NAME_MAX characters. And indeed, on Linux NAME_MAX is 255.

So, to answer you question, when the file system is created there is no space reserved for file names, but once you create a file NAME_MAX bytes are reserved for the name. Moreover, for the directory inode, my understanding is that at least on ext2/3/4 space is allocated in disk block (4 KB, unless you're doing something very strange) granularity as needed. I.e. a directory takes up at minimum 4 KB (plus an entry in the parent directory inode), and if the list of (filename, inode) pairs doesn't fit into that 4 KB (minus other overhead, e.g. directory permissions), it allocates a new 4 KB block to continue the list, and so forth (ext2/3 uses an indirect block scheme, whereas ext4 uses extents).

like image 188
janneb Avatar answered Oct 31 '22 20:10

janneb