Understanding Linux / UNIX Relative Pathname

Understanding Linux / UNIX Relative Pathname

In a Linux or UNIX file system, a relative pathname is a file or directory name that specifies the location of a file or directory in relation to the current working directory.

For example, consider the following directory structure:

refer‮t ‬o:lautturi.com
/
|-- home
|   |-- user1
|   |   |-- documents
|   |   |-- pictures
|   |-- user2
|   |   |-- documents
|   |   |-- pictures
|-- etc
|-- var
|-- tmp

If the current working directory is /home/user1, the relative pathname documents refers to the /home/user1/documents directory. If the current working directory is /home/user1/pictures, the relative pathname ../documents refers to the /home/user1/documents directory.

In a relative pathname, the . symbol refers to the current working directory, and the .. symbol refers to the parent directory of the current working directory. You can use these symbols to navigate the file system relative to the current working directory.

For example, the following relative pathnames refer to the following locations:

  • ./documents: The documents directory in the current working directory
  • ../documents: The documents directory in the parent directory of the current working directory
  • ../../etc: The etc directory in the grandparent directory of the current working directory

Relative pathnames are useful because they allow you to specify file and directory locations relative to the current working directory, rather than using absolute pathnames that specify the full path to a file or directory from the root of the file system. This can be more convenient and easier to read, especially when you are working with complex directory structures.

Keep in mind that relative pathnames are interpreted relative to the current working directory, so the same relative pathname may refer to different locations depending on the current working directory. Consult the documentation and online resources for more information on working with relative pathnames in a Linux or UNIX file system.

Created Time:2017-10-30 14:27:29  Author:lautturi