Path (computing)#Absolute and relative paths
{{Short description|Text that specifies a file system item in a directory structure}}
{{For|the environment variable|PATH (variable)}}
{{More citations needed|date=July 2008}}
{{Use dmy dates|date=March 2020}}
A path (or filepath, file path, pathname, or similar) is a text string that uniquely specifies an item in a hierarchical file system. Generally, a path is composed of directory names, special directory specifiers and optionally a filename, separated by delimiting text. The delimiter varies by operating system and in theory can be anything, but popular, modern systems use slash {{code|/}}, backslash {{code|\}}, or colon {{code|:}}.
A path can be either relative or absolute. A relative path includes information that is relative to a particular directory whereas an absolute path indicates a location relative to the system root directory, and therefore, does not depends on context like a relative path does. Often, a relative path is relative to the working directory. For example, in command {{code|ls f}}, {{code|f}} is a relative path to the file with that name in the working directory.
Paths are used extensively in computer science to represent the directory/file relationships common in modern operating systems and are essential in the construction of uniform resource locators (URLs).
History
Multics first introduced a hierarchical file system with directories (separated by ">") in the mid-1960s.{{cite conference |last1=Daley |first1=R.C. |last2=Neumann |first2=P.G. |book-title=Proceedings of the November 30--December 1, 1965, fall joint computer conference, Part I on XX - AFIPS '65 (Fall, part I) |title=A general-purpose file system for secondary storage |date=1965 |volume=Part I |pages=213–229 |doi=10.1145/1463891.1463915 |doi-access=free |s2cid=16236414}}
Around 1970, Unix introduced the slash character ("/") as its directory separator.
Originally, MS-DOS did not support directories, but when adding the feature, using the Unix standard of slash was not a good option since many existing commands used slash as the switch prefix. For example, dir /w
. In contrast, Unix uses dash {{code|-}} as the switch prefix. In this context, MS-DOS version 2.0 used backslash {{code|\}} for the path delimiter since it is similar to slash but did not conflict with existing commands. This convention continued into Windows in its shell Command Prompt. Eventually, PowerShell, was introduced to Windows that is slash-agnostic, allowing the use of either slash in a path.{{cite web |title=Why Windows Uses Backslashes and Everything else Uses Forward Slashes |date=10 February 2014 |url=https://www.howtogeek.com/181774/why-windows-uses-backslashes-and-everything-else-uses-forward-slashes/}}{{cite web |title=Why is the DOS path character ""? |url=https://learn.microsoft.com/archive/blogs/larryosterman/why-is-the-dos-path-character |date=24 June 2005}}
Path syntax
The following table describes the syntax of paths in notable operating systems and with notable aspects by shell.
{{Table alignment}}
class="wikitable col3center col4center col5center col6center" style="font-size:90%;" |
Context
! Root ! Path ! Working ! Parent ! Home ! Examples |
---|
Unix
| | | | | | |
DOS COMMAND.COM
| | | | | | |
OS/2 cmd.exe
| | | | | | |
Windows Command Prompt
| | | | | | |
Windows PowerShell
| | | | | |
|
TOPS-20 DCL
| | | | | | |
RSX-11 MCR
| | | | | | |
OpenVMS DCL
| | | | | |
|
Classic Mac OS | | | | | | |
ProDOS AppleSoft BASIC
| | | | | |
|
AmigaOS Amiga CLI / AmigaShell | | | empty | | | |
RISC OS ShellCLI
| note: &, %, and @ can also be used to reference the root | | | | | When filesystems with filename extensions are mounted, |
Symbian OS File manager
| | | | | | |
Domain/OS Shell
|
| | | | | |
MenuetOS CMD
| | | | | | |
Stratus VOS VOS command-line interpreter | | | | | | |
NonStop Kernel TACL Tandem Advanced Command Language | No root | | | none | | |
CP/M CCP
| | colspan="4" | no directory support, just user areas 0–F | align="left" | |
GS/OS
| note: prefix may be a number (0–31), | | | | | |
OpenHarmony (incl. HarmonyOS) exec{{cite web |title=Introduction to the Shell |website=GitHub |publisher=OpenAtom OpenHarmony |url=https://github.com/eDorUS/OpenHarmony_docs/blob/master/en/device-dev/kernel/kernel-small-debug-shell-overview.md |access-date=13 March 2024}}{{cite web |title=exec |website=GitHub |publisher=OpenAtom OpenHarmony |url=https://github.com/dashingcalico/OpenHarmony/blob/master/docs-en/kernel/exec.md |access-date=14 March 2024}}
|
| | | | | |
Japanese and Korean versions of Windows often display the '¥' character or the '₩' character instead of the directory separator. In such cases the code for a backslash is being drawn as these characters. Very early versions of MS-DOS replaced the backslash with these glyphs on the display to make it possible to display them by programs that only understood 7-bit ASCII (other characters such as the square brackets were replaced as well, see ISO 646, [https://www.microsoft.com/GLOBALDEV/Reference/dbcs/932.htm Windows Codepage 932 (Japanese Shift JIS)], and [https://www.microsoft.com/GLOBALDEV/Reference/dbcs/949.htm Codepage 949 (Korean)]). Although even the first version of Windows supported the 8-bit ISO-8859-1 character set which has the Yen sign at U+00A5, and modern versions of Windows supports Unicode which has the Won sign at U+20A9, much software will continue to display backslashes found in ASCII files this way to preserve backward compatibility.{{cite web |website=Sorting it all Out |title=When is a backslash not a backslash? |url=http://archives.miloush.net/michkap/archive/2005/09/17/469941.html}}
macOS, as a derivative of UNIX, uses UNIX paths internally. However, to preserve compatibility for software and familiarity for users, many portions of the GUI switch "/" typed by the user to ":" internally, and switch them back when displaying filenames (a ":" entered by the user is also changed into "/" but the inverse translation does not happen).
Paths in programming languages
Programming languages also use paths. E.g.: When a file is opened. Most programming languages use the path representation of the underlying operating system:
uxFile = fopen("project/readme.txt", "r")
winFile = fopen("C:\\Program Files\\bin\\config.bat", "r")
This direct access to the operating system paths can hinder the portability of programs. To support portable programs Java uses File.separator to distinguish between / and \ separated paths. Seed7 has a different approach for the path representation. In Seed7 all paths use the Unix path convention, independent of the operating system. Under windows a mapping takes place (e.g.: The path /c/users is mapped to c:\users).
Universal Naming Convention
{{Anchor|UNC}}
The Microsoft universal naming convention (UNC), a.k.a. uniform naming convention, a.k.a. network path, specifies a syntax to describe the location of a network resource, such as a shared file, directory, or printer. A UNC path has the general form:
\\ComputerName\SharedFolder\Resource
Some Windows interfaces allow or require UNC syntax for WebDAV share access, rather than a URL. The UNC syntax is extended{{cite web |title=DavGetHTTPFromUNCPath function |department=WebDAV: Windows |website=Microsoft Docs |series=series |date=13 October 2021 |url=https://learn.microsoft.com/windows/win32/api/davclnt/nf-davclnt-davgethttpfromuncpath |access-date=2023-10-04}} with optional components to denote use of SSL and TCP/IP port number, a WebDAV URL of http[s]://HostName[:Port]/SharedFolder/Resource
becomes
\\HostName[@SSL][@Port]\SharedFolder\Resource
When viewed remotely, the "SharedFolder" may have a name different from what a program on the server sees when opening "\SharedFolder". Instead, the SharedFolder name consists of an arbitrary name assigned to the folder when defining its "sharing".
Some Windows interfaces also accept the "Long UNC":
\\?\UNC\ComputerName\SharedFolder\Resource
Windows uses the following types of paths:
- local file system (LFS), such as
C:\File
- universal naming convention (UNC), such as
\\Server\Volume\File
or /
(at least in Windows 7 and later)[\Directory name] - "long" device path such as
\\?\C:\File
or\\?\UNC\Server\Volume\File
.{{cite web |title=File path formats on Windows systems |department=File and Stream I/O: .NET |website=Microsoft Docs |url=https://learn.microsoft.com/dotnet/standard/io/file-path-formats |access-date=2019-07-14}} This path points to the local file namespace and {{code|\\.\}} is a similar one that points to the local DOS device namespace. This format is also the "raw" or "uninterpreted" path, since it sends paths straight to the file system without converting {{mono|/}} to {{mono|\}} and interpreting names like {{mono|..}}. - Windows NT object manager
\\??\
-prefixed paths (global DOS namespace).{{cite web |title=winapi - Is there a difference between \??\ and \\?\ paths? |website=Stack Overflow |url=https://stackoverflow.com/questions/25090101/is-there-a-difference-between-and-paths}}{{cite web |title=Path prefixes \??\ and \\?\ |website=Stack Overflow |url=https://stackoverflow.com/a/46019856}}
In versions of Windows prior to Windows XP, only the APIs that accept "long" device paths could accept more than 260 characters.
The shell in Windows XP and Windows Vista, explorer.exe, allows path names up to 248 characters long.{{citation needed|date=February 2014}}
Since UNCs start with two backslashes, and the backslash is also used for string escaping and in regular expressions, this can result in extreme cases of leaning toothpick syndrome: an escaped string for a regular expression matching a UNC begins with 8 backslashes – \\\\\\\\
– because the string and regular expression both require escaping. This can be simplified by using raw strings, as in C#'s @"\\\\"
or Python's r'\\\\'
, or regular expression literals, as in Perl's qr{\\\\}
.
POSIX pathname definition
Most Unix-like systems use a similar syntax.{{cite web |title=UNC Definition |website=ComputerLanguage.com |url=https://www.computerlanguage.com/results.php?definition=UNC}}
POSIX allows treating a path beginning with two slashes in an implementation-defined manner,{{cite web |title=POSIX pathname resolution specification |url=https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11}}
though in other cases systems must treat multiple slashes as single slashes.{{cite web |title=POSIX pathname definition |url=http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_266}}
Many applications on Unix-like systems (for example, scp, rcp, and rsync) use resource definitions such as:
hostname:/directorypath/resource
or URI schemes with the service name (here 'smb'):
smb://hostname/directorypath/resource
Examples
=Unix=
The following examples are for typical, Unix-based file systems:
Given the working directory is {{code|/home/mark/}} and it contains subdirectory {{code|bobapples}}, relative paths to the subdirectory include {{code|./bobapples}} and {{code|bobapples}}, and the absolute path is {{code|/home/mark/bobapples}}. A command to change the working directory to the subdirectory:
A:\Temp\File.txt
is an absolute path that specifies a file named {{code|File.txt}} in the directory {{code|Temp}} which is in the root of drive {{code|A:}}:
C:..\File.txt
is a relative path that specifies file {{code|File.txt}} located in the parent of the working directory on drive {{code|C:}}:
Folder\SubFolder\File.txt
is a relative path that specifies file {{code|File.txt}} in directory {{code|SubFolder}} which is in directory {{code|Folder}} which is in the working directory of the current drive:
File.txt
is a relative path that specifies File.txt
in the working directory:
\\.\COM1
specifies the first serial port, COM1:
The following uses a path with slashes for directory delimiter:
C:\>more < C:/Windows/system.ini
; for 16-bit app support
[386Enh]
woafont=dosapp.fon
EGA80WOA.FON=EGA80WOA.FON
EGA40WOA.FON=EGA40WOA.FON
CGA80WOA.FON=CGA80WOA.FON
CGA40WOA.FON=CGA40WOA.FON
...
A path with forward slashes may need to be surrounded by double quotes to disambiguate from command-line switches. For example, {{code|dir /windows}} is invalid, but {{code|dir "/window"}} is valid. And {{code|cd}} is more lenient by allowing {{code|cd /windows}}.
See also
- {{Annotated link|basename}}
- {{Annotated link|Device file}}
- {{Annotated link|dirname}}
- {{Annotated link|Distributed file system}}
- {{Annotated link|Filename}}
- {{Annotated link|Filesystem Hierarchy Standard}}
- {{Annotated link|Fully qualified file name}}
- {{Annotated link|PATH (variable)}}
- {{Annotated link|URL}}
References
{{Reflist}}
External links
- [https://www.linfo.org/path.html Path Definition] - The Linux Information Project (LINFO)
- [https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file Naming Files, Paths, and Namespaces] - Local File Systems: Windows: Microsoft Docs
{{Computer files}}