Create folder from Windows then access from Bash in Windows 10 WSL

24,216

Solution 1

Update:

Since Windows 10 version 1903 it's possible to access Linux files from Windows with \\wsl$\. So you can run explorer.exe . from bash or enter \\wsl$\ directly in Explorer's address bar

See What’s new for WSL in Windows 10 version 1903?

Again there's still the warning that you must NOT access your Linux files via the AppData folder!

For older versions of Windows 10 see below


Linux files in WSL root file system are created specially with POSIX permissions stored in NTFS extended attributes. You can see that by running fsutil file layout lxss\root and notice the $EA stream there. If you create the files directly from Windows you'll mess up things because win32 apps knows nothing about Linux and its file metadata

The problem arises when, for example, you use a Windows app/tool to open, create and/or modify a file under your distro root: Since the file was created with a Windows tool, the file won't have any Linux file metadata (e.g. permissions, owner, access/update timestamps, etc.). Thus, to Linux, (which only receives Linux file metadata), the file may be reported as empty, may not even exist, or may have some metadata, but that metadata may not reflect the file's details resulted in the file's contents being corrupted.

Therefore MS has boldly warned that

DO NOT, under ANY circumstances, create and/or modify Linux files using Windows apps, tools, scripts, consoles, etc.

Creating/changing Linux files from Windows will likely result in data corruption and/or damage your Linux environment requiring you to uninstall & reinstall your distro!

Do not change Linux files using Windows apps and tools

Files that are only relevant to Linux should be created only from WSL. If you want to create files that are accessible both systems then create them outside the WSL root and access from WSL via the mount point in /mnt like /mnt/drive/path

If you want to create files on Windows file systems but retain the case sensibility then use

fsutil file setCaseSensitiveInfo <directory name> enable

The flag can also be checked with fsutil file queryCaseSensitiveInfo <directory name>

See also How to access linux/Ubuntu files from Windows 10 WSL?

Solution 2

You can not create folders in Ubuntu and access them from Windows. But you can do opposite.

You can access your Windows' drives at /mnt. If you do ls /mnt you will see all your drives. So /mnt/c is path of your C drive. Similarly /mnt/c/Users/username/Desktop can give you access to your desktop in Bash.

So just reach to your desired path using Bash and access windows files & folders. For more details on making quick access to your windows files, use symlinks, details can be found here

Solution 3

Invert your thinking: If you want to create files using Windows apps & tools, and access them from Bash, then create those files etc. in your Windows filesystem and access/copy them from Bash.

Here's an example of creating a file in PowerShell, then calling Bash to copy the created file into the home folder, and then calling bash to write the contents of the file:

PS C:\dev\temp> Set-Content ./hello.txt -Value "Hello, World!" -Encoding ASCII
PS C:\dev\temp> cat .\hello.txt
Hello, World!
PS C:\dev\temp> bash -c "cp /mnt/c/dev/temp/hello.txt ~/"
PS C:\dev\temp> bash -c "cat /mnt/c/dev/temp/hello.txt"
Hello, World!

HTH.

Share:
24,216

Related videos on Youtube

t q
Author by

t q

here to learn

Updated on September 18, 2022

Comments

  • t q
    t q over 1 year

    I am running Ubuntu from Windows.

    When I create a folder from Windows called newFolder in the root directory C:\Users\myuser\AppData\Local\lxss\root\Newfolder then from Bash when I run ls I don't see the new folder that was just created.

    How can I fix this so that I can create folders and files from Windows then access them from Bash?