在一个目录中保存多少文件有关系吗?如果是,一个目录中有多少文件是太多的,拥有太多文件的影响是什么?(这是在Linux服务器上。)

Background: I have a photo album website, and every image uploaded is renamed to an 8-hex-digit id (say, a58f375c.jpg). This is to avoid filename conflicts (if lots of "IMG0001.JPG" files are uploaded, for example). The original filename and any useful metadata is stored in a database. Right now, I have somewhere around 1500 files in the images directory. This makes listing the files in the directory (through FTP or SSH client) take a few seconds. But I can't see that it has any effect other than that. In particular, there doesn't seem to be any impact on how quickly an image file is served to the user.

我想过通过创建16个子目录来减少图像的数量:0-9和a-f。然后我将根据文件名的第一个十六进制数字将图像移动到子目录中。但是除了偶尔通过FTP/SSH列出目录之外,我不确定这样做是否有任何理由。


当前回答

上面的大多数答案都没有说明,对于最初的问题,没有“一刀切”的答案。

In today's environment we have a large conglomerate of different hardware and software -- some is 32 bit, some is 64 bit, some is cutting edge and some is tried and true - reliable and never changing. Added to that is a variety of older and newer hardware, older and newer OSes, different vendors (Windows, Unixes, Apple, etc.) and a myriad of utilities and servers that go along. As hardware has improved and software is converted to 64 bit compatibility, there has necessarily been considerable delay in getting all the pieces of this very large and complex world to play nicely with the rapid pace of changes.

恕我直言,没有一种方法可以解决问题。解决办法是研究各种可能性,然后通过反复试验找到最适合你特定需求的方法。每个用户必须确定什么适合他们的系统,而不是使用千篇一律的方法。

I for example have a media server with a few very large files. The result is only about 400 files filling a 3 TB drive. Only 1% of the inodes are used but 95% of the total space is used. Someone else, with a lot of smaller files may run out of inodes before they come near to filling the space. (On ext4 filesystems as a rule of thumb, 1 inode is used for each file/directory.) While theoretically the total number of files that may be contained within a directory is nearly infinite, practicality determines that the overall usage determine realistic units, not just filesystem capabilities.

我希望以上所有不同的答案都能促进思考和解决问题,而不是成为进步的不可逾越的障碍。

其他回答

Ext3实际上有目录大小限制,它们取决于文件系统的块大小。没有每个目录的文件“最大数量”,而是每个目录的“用于存储文件条目的最大块数量”。具体来说,目录本身的大小不能超过高度为3的b-树,并且树的扇出取决于块大小。有关详细信息,请参见此链接。

https://www.mail-archive.com/cwelug@googlegroups.com/msg01944.html

我最近在一个格式化为2K块的文件系统上就遇到过这种情况,它莫名其妙地得到目录已满的内核消息警告:ext3_dx_add_entry:目录索引已满!当我从另一个ext3文件系统复制时。在我的例子中,一个只有480,000个文件的目录无法复制到目标。

如果实现目录分区方案所涉及的时间是最少的,我赞成它。当您第一次调试涉及通过控制台操作10,000个文件目录的问题时,您将能够理解。

例如,F-Spot将照片文件存储为YYYY\MM\DD\filename。ext,这意味着在手动操作我的~20000张照片集合时,我必须处理的最大目录大约有800个文件。这也使文件更容易从第三方应用程序中浏览。永远不要以为只有你的软件会访问你的软件文件。

我遇到的最大问题是在32位系统上。一旦你通过了一个特定的数字,像'ls'这样的工具就会停止工作。

一旦您通过了这个障碍,试图对该目录做任何事情都将成为一个巨大的问题。

问题归结为你将如何处理这些文件。

在Windows下,对于我来说,在资源管理器中打开任何超过2k个文件的目录都比较缓慢。如果它们都是图像文件,在缩略图视图中,超过1k的文件往往打开得非常慢。

系统规定的上限曾一度是32767个。现在它更高了,但即使如此,在大多数情况下,一次处理的文件也太多了。

我有一个目录,里面有88914个文件。就像你自己,这是用于存储缩略图和在Linux服务器上。

通过FTP或php函数列出的文件是缓慢的,但是在显示文件时也有性能上的影响。例如,www.website.com/thumbdir/gh3hg4h2b4h234b3h2.jpg的等待时间为200-400毫秒。在另一个网站上,我有一个目录下大约100个文件,在大约40毫秒的等待后,图像就显示出来了。

我给出了这个答案,就像大多数人刚刚写了如何执行目录搜索函数一样,你不会在拇指文件夹上使用它——只是静态地显示文件,但会对如何实际使用文件的性能感兴趣。