Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was wondering what differences and relations are between file descriptors and file names. Are they all used to access files? If yes, in the same way?

For example, /dev/fd/0, /dev/stdin, and /proc/self/fd/0 are all links to /dev/pts/2. Are these four file descriptors, or file names?

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

All four of /dev/fd/0, /dev/stdin, /proc/self/fd/0 and /dev/pts/2 are file names, as are /////dev/../dev/fd//0, /bin/sh, /etc/fstab, /fioejfoeijf, etc. All but that last example are likely to be the name of an existing file on your machine. A file name is a string that can designate a file on your filesystem; under Linux, any string that does not contain a null byte and that is at most 4096 bytes long is a valid file name. Many of these names are equivalent, e.g. /bin/sh is equivalent to ///bin/sh, /bin/../bin/sh (assuming /bin is an existing directory), etc. All the examples I've given so far are absolute file names; there are also relative file names, which do not begin with a / and whose meaning depends on the current directory.

The terminology surrounding file names isn't universal; sometimes “file name” means a full path to a file, and sometimes it means the name of a directory entry. The POSIX terminology is “filename” or “pathname component” for the name of a directory entry, and “pathname” for a full path.

A file descriptor designates an open file in a particular process. The kernel maintains a table of file descriptors for each process. Each entry in the file descriptor table indicates what to do if the process requests reading, writing and other operations on the file descriptor.

File descriptors may correspond to a file and have an associated name, but not all of them do. For those that do, the file may be a regular file, a directory, a device file or a named pipe (also called FIFO) (the kind created by mkfifo); some systems have further possibilities such as unix sockets and doors. Examples of file descriptors that don't have an associated named file include pipes (the kind created by the pipe) and networking sockets.

/dev/fd/0, /dev/stdin and /proc/self/fd/0 are file names (all equivalent) with a peculiar meaning: they all designate whichever file is currently accessed via file descriptor 0. When a process opens these, the kernel copies the entry with index 0 in the file descriptor descriptor table to a new descriptor. Opening any of these files is equivalent to calling dup(0). The named files are a way to indirectly get a process to use one of its already-open files rather than open a new file; they are mostly useful to pass on a program's command line, where the program expects the name of a file to open.

share|improve this answer
add comment

Filename is just a name of the file on filesystem, nothing more - it's just a string.

File descriptor is some kind of object, from where you can read and/or write. That is opened and ready file. There are not only file descriptors - stdin, stdout and stderr are also descriptors, you can write and read from them using absolutely the same functions as with files (except you can't for example seek). Other examples of descriptors which are not files are: named pipes and network sockets.

In code, file descriptor is a variable, pointer to something in kernel, which represents file object.

For example, /dev/fd/0, /dev/stdin, /proc/self/fd/0 are all links to /dev/pts/2. Are these four file descriptors, or file names?

This all are pseudo-files, which can be useful for some scripts or programs. You can open them and open() would return you a file descriptor.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.