A file descriptor is a number your operating system assigns to an open file so it can keep track of it

When you open a file — whether you click it in a folder, a program reads it in the background, or you type a command to access it — your operating system does not hand you the file itself. Instead, it gives you a number. That number is the file descriptor. It is a reference, like a ticket stub at a coat check: you hand over the ticket, the attendant knows exactly which coat to retrieve.

The operating system uses file descriptors internally to manage which files are open, where they are on your disk, and what you are allowed to do with them. When a program wants to read data from a file, it tells the operating system "I want to read from file descriptor 3" — and the operating system knows which file that refers to and whether the program has permission to read it.

File descriptors exist on every operating system: Windows, macOS, and Linux all use them. You rarely see them directly unless you are writing code or troubleshooting at a technical level, but they are running in the background every time you save a document or load a photo.

Key Takeaways

  • A file descriptor is a number assigned by your operating system to represent an open file, so the system can track and manage it.
  • Each program running on your computer has its own set of file descriptors, numbered starting from 0, and each number refers to a different open file or data stream.
  • File descriptors are how your operating system enforces permissions — it checks whether a program has the right to read, write, or execute a file before allowing the operation.
  • Most users never interact with file descriptors directly, but they are essential to how programs read and write files without interfering with each other.

How file descriptors are numbered and assigned

Every program running on your computer gets its own set of file descriptors, and they always start numbering from 0. The first three are reserved: 0 is standard input (where data comes in), 1 is standard output (where results go), and 2 is standard error (where error messages go). When a program opens a fourth file, it gets file descriptor 3. The fifth gets 4, and so on.

The numbering restarts for each program. If you have two programs open at the same time, they both have their own file descriptor 3 — but they refer to different files, because each program's file descriptors are separate. This separation is how your operating system prevents one program from accidentally reading or writing to another program's files.

File descriptors are temporary. When you close a file, the operating system releases that file descriptor number so it can be reused the next time the program opens a different file. If you close a document and then open a different one, the new file might get the same descriptor number as the old one had — because the old number is no longer in use.

Why the operating system uses file descriptors instead of file names

You might wonder why the operating system does not just use the file name directly — why add a middle step? The answer is speed and security. A file name like "Documents/Taxes/2024_Return.pdf" is long and has to be looked up on disk every time. A file descriptor is just a small number that the operating system can look up when ready in a table it keeps in memory.

File descriptors also enforce permissions. When a program opens a file, the operating system checks whether that program has permission to read it, write to it, or run it. If it does, the operating system creates a file descriptor. If it does not, the operating system refuses to create one and the program gets an error. Once a file descriptor exists, the operating system trusts that the program has already been vetted — it does not have to check permissions again on every single read or write operation.

This design also prevents a program from changing what a file descriptor points to after it has opened the file. If a program opens "Document.txt" and gets file descriptor 5, that descriptor always refers to the same file, even if someone renames the file or moves it to a different folder while the program is still using it. The program keeps working without interruption.

Where you might encounter file descriptors in practice

Most of the time, file descriptors are invisible. You click a file, it opens, and you do not think about the number the operating system assigned to it. But in a few situations you might see them or need to understand them.

If you write code or use the command line, you will work with file descriptors directly. A programmer might write code that says "open this file and store the file descriptor in a variable" so the program can read from it later. In the command line on macOS or Linux, you can see which files a running program has open by looking at its file descriptors.

If a program crashes or stops responding, a technical support person might ask you to check which files it had open — they do this by looking at the file descriptors. If a program runs out of file descriptors (because it opened too many files and did not close them), it will fail with an error message about "too many open files." That error means the program hit the limit of how many file descriptors the operating system allows it to have at once.

File descriptor limits and what happens when you hit them

Your operating system sets a limit on how many file descriptors a single program can have open at the same time. On most systems, this limit is somewhere between 256 and 1024 for regular users, though system administrators can raise it. This limit exists to prevent a runaway program from consuming all the operating system's resources.

If a program opens files but never closes them, it will eventually hit this limit. When it does, the program cannot open any more files and will usually crash or display an error. This is a common problem in programs that process many files in a loop — if the programmer forgets to close each file after reading it, the program will fail partway through.

You cannot usually fix this yourself as a user. If a program keeps hitting file descriptor limits, the problem is in how the program was written. The programmer needs to make sure the program closes files when it is done with them. A technical support person might be able to raise the system-wide limit temporarily, but that is a workaround, not a solution.

How file descriptors relate to file organization

Understanding file descriptors helps explain why organizing your files matters. When you create a clear folder structure and name files consistently, you make it easier for programs to find and open the files they need. A program that has to search through a disorganized folder structure takes longer to locate files and may open more file descriptors than necessary while searching.

File descriptors also explain why you should close programs when you are done with them. Every open program is holding file descriptors for the files it has open. If you leave many programs running, you are using up file descriptors across your system. Closing programs you are not using frees those descriptors back up and keeps your system running smoothly.

When you delete a file, the operating system closes any file descriptors pointing to it. If a program still has that file open, the program can keep reading from the file descriptor even though the file is gone from your folder — the data is still on disk until the program closes the descriptor. This is why you can sometimes delete a file that appears to be in use; the operating system allows it as long as no program is actively preventing the deletion.

Frequently Asked Questions

Can I see what file descriptors a program has open?

On macOS and Linux, you can use the command line tool lsof (list open files) to see which files a running program has open and what file descriptors it is using. On Windows, you can use the Resource Monitor or Process Explorer to see open files. Most users do not need to do this, but it is useful for troubleshooting when a program is behaving strangely.

What does "too many open files" error mean?

This error means a program has hit the file descriptor limit set by your operating system. The program opened more files than it is allowed to have open at the same time and cannot open any more. Usually this happens because the program has a bug and is not closing files after it finishes reading them. Restarting the program often fixes it temporarily.

Do I need to manually close file descriptors?

No. When you close a program, the operating system automatically closes all the file descriptors that program was using. When you close a file in a program, the program tells the operating system to close the file descriptor. You do not manage file descriptors yourself unless you are writing code.

Is there a difference between a file descriptor and a file handle?

File descriptor and file handle are often used interchangeably, though technically a file handle is the broader term. A file descriptor is the number; a file handle is the reference that includes the descriptor plus some additional information the operating system tracks. For most purposes, you can treat them as the same thing.

Why do file descriptors start at 0 instead of 1?

This is a convention from early Unix systems, and it has stuck around for decades. Starting at 0 makes it easier for programmers to work with file descriptors in code, because arrays and lists in most programming languages also start counting at 0. It is not a technical requirement, just a standard that makes programming simpler.