I spent more time than I care to admit trying to figure out how to make symlinks generated through Windows Ubuntu (also known as WSL, Windows Subsystem for Linux) recognizable in Windows.
I kept using this command:
ln -s my-actual-dir my-symlink. And it would create a file instead of a directory. Also, it would be readable from Windows Ubuntu but not in Windows 10. This was an issue because I was trying to create a symlink to a theme folder inside the WordPress theme directory. And although the file was there, there was no way for me to activate the theme from the WordPress dashboard because it wasn’t really a folder, just a file.
Symlinks created using the
mklink command are recognized in Windows 10 and Windows Ubuntu sooo I decided to just use it instead of trying to figure out why
ln -s <target> <link> was not working. Fortunately, I found this
mklink.sh bash script a very long time ago (thanks to stackoverflow) in 2017.
What I did from within Windows Ubuntu:
- First I created the
mklinkbash script and stored in my
cd ~ //(to my home directory) mkdir bin // The internet said it makes sense to store this file in $HOME/bin directory cd bin touch mklink.sh nano mklink.sh
- Then I used the editor and paste in everything from this gist:
- Then I added this line:
export PATH="$HOME/bin:$PATH"to my
- Navigate back to your home directory or wherever you’ve stored an alias file and create an alias for the
cd ~ nano .bash_aliases alias mklink=" . mklink.sh"
- Save the file, close the terminal and reopen it. Remember you can’t modify WSL files in the Windows environment which is why I use the nano command to edit files within the terminal. Don’t you dare try to modify that file from a text editor in Windows!
Try creating a symlink from WSL now using this command:
mklink <link> <target>. Remember the
<target>is the location where all the files actually exists and
< link>is where you want to create a symlink which will mirror everything from the
Here is a screen recording of me creating a symlink using this script, and some screenshots at the bottom!
Helpful tips: What to do when…
The pwd is inside of WSL directories:
\\wsl$\Ubuntu\home\badgirlriri), and you’re attempting to create a symlink from a target directory on Windows, you can create a symlink using an absolute path:
mklink /projects/mklink-test/symlink1 /projects/mklink-test/target
The pwd is inside of your WSL symlink
ln -s /mnt/c/projects/ myProjects) and which results in a directory listing like:
myProjects -> /mnt/c/projects,then when your pwd is
~/myProjects, you can use relative paths:
mklink mklink-test/symlink2 mklink-test/target
The pwd is inside of a Windows directory
/mnt/c/Users/Rihanna), you can create a symlink using an absolute path or a relative path (if you’re in the directory) using one of the following commands.
## Absolute Path from pwd /mnt/c mklink projects/mklink-test/symlink3 projects/mklink-test/target ## Relative Path from pwd /mnt/c/projects mklink mklink-test/symlink4 mklink-test/target ## Relative Path from pwd /mnt/c/projects to a completely different directory referenced by the absolute path mklink /newProjects/mklink-test-2/symlink5 mklink-test/target # note that the link path is absolute here
This post was made possible by a lot of google searching, a few links are below: