WSL wslpath

Posted on Monday, March 2, 2026


 


In Cygwin I can drag and drop a windows file or folder and have its path updated to linuxy version.  In WSL they do not have this feature yet.

So…

In WSL you do have a path to local drives.  It is at /mnt

 

I can do something like this

 

> ls /mnt/d/pictures/

 

And that works.

But if I drag and drop the D:\pictures folder into Ubuntu WSL I get…

D:\pictures

 

Which is not a converted linuxy path like Cygwin does. 
So its not going to work as is.

However there is a tool
wslpath
we can use to convert it. Its not as convenient but it works

Let’s say we want to change directory
Type in this first part of the command

 

> cd $(wslpath "

 

 

Then drag and drop in the folder and add in a ") to close it



That works…

but it’s a bit of a pain.
I would rather have some kind of automation.

here is the best I came up with, replacing the cd command.
This still requires you to put the win path in quotes because linux will ignore the single \ in the path

Update .bashrc

 

> vi ~/.bashrc

 

And add this to the bottom

 

cd() {

    if [ $# -eq 1 ] && [[ "$1" =~ ^[a-zA-Z]:\\ || "$1" =~ ^[a-zA-Z]:/ || "$1" =~ ^\\\\ ]]; then
        # Looks like Windows path (with \ or / or UNC \\server\share)
        local winpath="$1"
 
        # Strip outer quotes if present
        winpath="${winpath%\'}"
        winpath="${winpath#\'}"
        winpath="${winpath%\"}"
        winpath="${winpath#\"}"

        local linuxpath
        linuxpath=$(wslpath -u -- "$winpath" 2>/dev/null) 

        if [ $? -eq 0 ] && [ -d "$linuxpath" ]; then
           builtin cd -- "$linuxpath"
        else
            echo "cd: cannot access '$1' (wslpath failed or not a directory)"
            return 1
        fi
    else
        # Normal cd behavior
        builtin cd "$@"
    fi
}

 

 


Now I can either restart my terminal or run

 

> source ~/.bashrc

 

To pick up the changes

Now let me try it

 

> cd "D:\pictures"

 

 

That works!

References

 

No comments:

Post a Comment