Technology Sharing

Linux Basics: III. Relative and absolute paths

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

3. Relative path and absolute path

  • Strictly speaking, the file name is composed of directory + file name.

Windows

  • Absolute path

    • An absolute path starts with the drive letter, for example:C:UsersUsernameDocumentsfile.txt
    • In Windows, the path separator is a backslash.
    • An absolute path refers to the complete location of a file or directory, regardless of the current working directory.
  • relative path

    • A relative path is a path relative to the current working directory, excluding the drive letter.
    • In Windows,. Indicates the current directory,.. Indicates the parent directory.
    • Example: If the current working directory is C:UsersUsername,but Documentsfile.txt Equivalent toC:UsersUsernameDocumentsfile.txt
    • Example: If the current working directory is C:UsersUsername,but .Documentsfile.txt Equivalent toC:UsersUsernameDocumentsfile.txt

Linux

  • Absolute path

    • An absolute path is counted from the root (/) and will not be ambiguous at any time, for example:/usr/include/stdio.h
    • In Linux, the path separator is a forward slash. /
    • An absolute path is always counted from the root directory, and the path uniquely determines the location of a file or directory.
  • relative path

    • A relative path is a path relative to the current working directory, excluding the root directory portion.
    • In Linux,. Indicates the current directory,.. Indicates the parent directory.
    • Example: If the current working directory is /usr,but include/stdio.h Equivalent to/usr/include/stdio.h

Comparison between the two

  • Path separator

    • Windows uses backslash , Linux uses forward slashes /
  • Path resolution

    • Windows is case-insensitive when processing paths, but the path separators are different.
    • Linux paths are strictly case-sensitive.
  • usage habit

    • When programmers write code, they often use absolute paths to ensure the accuracy and portability of files or directories.
    • Relative paths are more commonly used in command line operations or simple scripts to reference paths relative to the current working directory.

Path separator in relative path - . or ./

  • "./user/test.sh" is equivalent to "user/test.sh"

  • The presence or absence of a dot in the path usually has no effect, especially in most modern operating systems and command line environments. This is because:

  • Default current directory: When you specify a relative path, the system will default to looking for the file or directory in the current working directory. So, Documentsfile.txt and .Documentsfile.txt point to the same file location in most cases.

  • Display the current directory: Sometimes, in order to clearly indicate the current directory, or to avoid confusion in complex commands, . is used explicitly. Doing so can improve the readability of the code and the clarity of the command.

  • Cross-platform adaptability: In cross-platform development or scripting, explicitly using . ensures that path separators are interpreted correctly on different operating systems. For example, use on Windows and / on Unix or Linux.

  • In general, whether to use . depends on personal or team preference and coding standards. In most cases, the system automatically handles relative paths.Therefore, . is not required, but it can help clarify the starting point of the path.