Files – Understanding Filename and Pathname of a File

filenamesfiles

It is said that the filename of a file is a pointer pointing to the inode of the file.
Before knowing that, I thought it was the pathname of a file that does that job.

I am not sure how a pathname and a filename are defined in Unix.
For example, is it correct that
/home/tim/tim.pdf is the pathname of the file with file name tim.pdf.

I wonder what the difference between the filename and the pathname of a file? Thanks.

Best Answer

In POSIX terminology, a filename is the name of a directory entry. It consists of a non-empty sequence of bytes other than / or null. The term “pathname component” is synonymous with “filename”. A pathname is a string that can contain any non-null byte and that designates a way to locate a file. A pathname consists of a series of filenames, in which all but the last refer to a directory. Pathname resolution is the process of locating a file from a pathname.

For example, /home/tim/tim.pdf is a pathname. The last component of that pathname tim.pdf is a filename; it is the name of an entry in the directory whose pathname is /home/tim. The filename tim is itself the name of an entry in the directory whose pathname is /home; this file happens to be a directory. tim.pdf is also a pathname: any filename is a pathname that happens to have a single component and designates the file with that name in the current directory.

/ is a pathname that refers to the root directory. . is a filename that exists in every directory and refers to that directory itself. . is a pathname which happens to contain a single component and refers to the current directory.

You can think of a filename as a pointer within a directory to an inode that is a file in that directory. A pathname is a specification of where to find an inode. Each component of a pathname is a filename that points to an inode in the directory reached so far (if the file exists). Pathnames either start at the root directory (if they are absolute pathnames, beginning with /) or at the current directory (if they are relative pathnames, not beginning with /).

Note that in many texts, the word “filename” (or “file name”) is used to mean what POSIX calls a pathname.