Mac OS X,  Mac OS X Server,  Ubuntu

Maximum Files in Mac OS X

By default, the maximum number of files that Mac OS X can open is set to 12,288 and the maximum number of files a given process can open is 10,240. There are a number of applications that might try to exceed this number, whether ill advised or not. And so if you would like to raise the limit (given that you have enough CPU and RAM to do so) then you can use the sysctl command to do so. To view the sysctl values for maximum files use the following command:
sysctl -A | grep kern.maxfiles

This will show you the kern.maxfiles and kern.maxfilesperproc variables. There are two ways to increase this limit. The first is using the sysctl command itself. To do so you would use the -w option. For example, to set kern.maxfiles to 20,480 you would use the following command:
sysctl -w kern.maxfiles=20480

The only problem with this method is that upon the next reboot the kern.maxfiles will go back to the default value. So to set it permanently you would edit the sysctl.conf file and reboot. Of course, this isn’t as easy as you might think since the sysctl.conf file doesn’t actually exist by default. Therefore you’d create it in /etc using a command like the following:
touch /etc/sysctl.conf

Then you’d add in the contents that you’d like to have, for example:
kern.maxfiles=20480 kern.maxfilesperproc=24576

Once you’ve done this, the kernel itself will have a maximum number of files but the shell might not. And since most processes that will take up this many files are going to be initiated by the shell you’re gonna’ want to increase that. You can change shell limits using the ulimit command. To do so:
ulimit -S -n 2048

Just a warning that unless you have a high number of transactions you DO NOT need to do this. If you have a process that is trying to exceed the default maximum number of files first think about why it’s trying to do so. Then if it should actually be doing it, go ahead and increase the numbers and see if you are able to fix your problem. But remember that every computer has a finite amount of resources, and by increasing something like the maximum number of files that can be opened, you are going to allow the computer to potentially use more resources than it should.

Finally, keep in mind that many processes will have built in controls for how many resources they are able to take up. For example, to control PHP’s number of files and amount of time a given file transfer is allowable you would look to the PHP.ini file rather than the kernel. The number of files a process can have is the lowest common denominator between the upper limit defined by the process, the upper limit allowable by the shell (if it’s invoked by the shell – and it probably is) and finally the upper limit allowable by the kernel. Most processes worth running are a little complicated to manage…

Note: The Linux equivalent to kern.maxfiles is fs.max-files.