LinuxCommandLibrary

wslpath

Convert paths between Windows and WSL

SYNOPSIS

wslpath [-a] [-u|-w|-m] <path>

PARAMETERS

-a
    Forces the output path to be an absolute path. This option ensures that the converted path is fully qualified, rather than relative.

-u
    Converts the given Windows path to a WSL (Linux) format path. For example, C:\Users\file.txt becomes /mnt/c/Users/file.txt.

-w
    Converts the given WSL (Linux) path to a Windows format path using backslashes. For example, /mnt/c/Users/file.txt becomes C:\Users\file.txt.

-m
    Converts the given WSL (Linux) path to a Windows format path using mixed slashes (forward slashes for directories, backslashes for drive letters). For example, /mnt/c/Users/file.txt becomes C:/Users/file.txt. This can be useful for applications that expect Windows paths but are more tolerant of forward slashes.

<path>
    The path to be converted. This can be either a Windows path or a WSL/Linux path, depending on the conversion option used.

DESCRIPTION

wslpath is a command-line utility integral to the Windows Subsystem for Linux (WSL), designed to facilitate seamless interoperability between Linux and Windows file systems. Its primary function is to translate file paths from a WSL/Linux format to a Windows-compatible format, and vice-versa. This is crucial for scripts and applications that need to interact across both environments, allowing users to specify paths in a way that the target operating system can understand.

For instance, a Linux path like /home/user/document.txt might be converted to \\wsl$\Ubuntu\home\user\document.txt (for WSL2 shares) or C:\path\to\file.txt if it's on a mounted Windows drive. Conversely, a Windows path such as C:\Users\user\Desktop can be transformed into its Linux equivalent, typically /mnt/c/Users/user/Desktop. This utility is indispensable for bridging the gap between the distinct path conventions of Linux and Windows, making cross-platform scripting and tool integration significantly smoother.

CAVEATS

When converting between environments, wslpath relies on the mounted file system structure. Windows drives are typically mounted under /mnt/ in WSL (e.g., C: becomes /mnt/c).

While highly effective, complex network paths (UNC paths) or paths containing special characters might sometimes require careful handling or specific formatting. For WSL2, Linux file paths (e.g., /home/user) are often exposed to Windows via a 9P protocol share, accessible as \\wsl$\<distro_name>, which wslpath can also translate.

UNC PATH SUPPORT

wslpath is capable of handling Universal Naming Convention (UNC) paths for network shares. This means it can convert Windows UNC paths like \\server\share\folder to their WSL equivalent, such as /mnt/unc/server/share/folder, and vice-versa. This feature is particularly valuable for integrating WSL environments into corporate networks or working with shared resources.

SCRIPTING UTILITY

One of the most common and powerful uses of wslpath is within shell scripts. It allows scripts running in the WSL environment to correctly reference files on Windows drives or pass Windows-formatted paths to native Windows applications, and similarly, enable Windows scripts to interact with files within the WSL filesystem. This ensures that command-line tools and utilities can operate without manual path adjustments, streamlining complex automation tasks.

HISTORY

The wslpath utility was introduced early in the development of the Windows Subsystem for Linux to address a fundamental challenge: enabling seamless interaction between the distinctly different file system path conventions of Linux and Windows. As WSL evolved, particularly with the introduction of WSL2 and its more integrated architecture, wslpath remained a critical tool for explicit path conversions, supporting scripting and cross-environment workflows. Its consistent functionality underscores its importance in the WSL ecosystem, bridging the gap for developers and users working across both OS environments.

SEE ALSO

wsl(1), wsl.exe (Windows command), explorer.exe (Windows command)

Copied to clipboard