Linux 上的最大文件/目录数? [英] Maximum number of files/directories on Linux?

查看:109
本文介绍了Linux 上的最大文件/目录数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 LAMP 在线商店,这将允许管理员为每件商品上传多张图片.

I'm developing a LAMP online store, which will allow admins to upload multiple images for each item.

我担心的是 - 马上就会有 20000 个项目,这意味着大约 60000 张图片.

My concern is - right off the bat there will be 20000 items meaning roughly 60000 images.

问题:

  1. Linux 上文件和/或目录的最大数量是多少?

  1. 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] 文件系统具有固定的最大索引节点数;每个文件或目录都需要一个 inode.您可以使用 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 启用),在大型目录中查找并不是什么大问题.但是,请注意,许多管理工具(例如 lsrm)可能很难处理包含过多文件的目录.因此,建议将您的文件拆分,以便任何给定目录中的项目不超过几百到一千个.一种简单的方法是对您使用的任何 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆