Linux上文件/目录的最大数量? [英] Maximum number of files/directories on Linux?
问题描述
我正在开发一个LAMP在线商店,这将允许管理员为每个项目上传多个图像.
I'm developing a LAMP online store, which will allow admins to upload multiple images for each item.
我担心的是-马上就会有2万个项目,大约意味着6万张图片.
My concern is - right off the bat there will be 20000 items meaning roughly 60000 images.
问题:
-
Linux上文件和/或目录的最大数量是多少?
What is the maximum number of files and/or directories on Linux?
处理这种情况的常用方法是什么(最佳实践)?
What is the usual way of handling this situation (best practice)?
我的想法是根据每个项目的唯一ID为每个项目创建一个目录,但是我在主 uploads 目录中仍然有20000个目录,并且它将随着旧项目的增长而无限期增长将不会被删除.
My idea was to make a directory for each item, based on its unique ID, but then I'd still have 20000 directories in a main uploads directory, and it will grow indefinitely as old items won't be removed.
感谢您的帮助.
推荐答案
ext [234]文件系统具有固定的最大i节点数;每个文件或目录都需要一个索引节点.您可以使用df -i
查看当前计数和限制.例如,在使用默认设置创建的15GB ext3文件系统上:
ext[234] filesystems have a fixed maximum number of inodes; every file or directory requires one inode. You can see the current count and limits with df -i
. For example, on a 15GB ext3 filesystem, created with the default settings:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/xvda 1933312 134815 1798497 7% /
除此以外,目录没有特别限制;请记住,尽管每个文件或目录都至少需要一个文件系统块(通常为4KB),即使它是一个只有单个项目的目录.
There's no limit on directories in particular beyond this; keep in mind that every file or directory requires at least one filesystem block (typically 4KB), though, even if it's a directory with only a single item in it.
但是,如您所见,80,000个inode不太可能成为问题.而且,使用dir_index
选项(可在tune2fs
中启用),在大型目录中的查找并不是一件大事.但是,请注意,许多管理工具(例如ls
或rm
)可能很难处理其中包含过多文件的目录.因此,建议对文件进行拆分,以使给定目录中的项目数不超过几百到一千.一种简单的方法是对您正在使用的ID进行哈希处理,并将前几个十六进制数字用作中间目录.
As you can see, though, 80,000 inodes is unlikely to be a problem. And with the dir_index
option (enablable with tune2fs
), lookups in large directories aren't too much of a big deal. However, note that many administrative tools (such as ls
or rm
) can have a hard time dealing with directories with too many files in them. As such, it's recommended to split your files up so that you don't have more than a few hundred to a thousand items in any given directory. An easy way to do this is to hash whatever ID you're using, and use the first few hex digits as intermediate directories.
例如,假设您的ID为12345,并且哈希为'DEADBEEF02842.......'
.您可以将文件存储在/storage/root/d/e/12345
下.现在,您已将每个目录中的文件数减少了1/256.
For example, say you have item ID 12345, and it hashes to 'DEADBEEF02842.......'
. You might store your files under /storage/root/d/e/12345
. You've now cut the number of files in each directory by 1/256th.
这篇关于Linux上文件/目录的最大数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!