There is one hard-and-fast rule when it comes to Bash on Windows:
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!
Note: Your “Linux files” are any of the files and folders under %localappdata%\lxss – which is where the Linux filesystem – distro and your own files – are stored on your drive
Do not create/modify Linux files from Windows apps/tools!
Why is this?
If WSL can’t find Linux file metadata for a given file, it assumes the file is corrupted/damaged, and will likely fail to open/read the file correctly, causing apps, tools, etc. to fail and in some cases, delete files seen as “corrupt”!
File metadata (e.g. permissions, ownership, timestamps, etc.) is represented differently in Linux than in Windows. Because stores your Linux files in an NTFS folder, WSL calculates and persists each Linux file’s metadata in its NTFS extended attributes
However, Windows apps do not know how to (nor that they should) re-calculate & persist this Linux metadata each time they create/modify a file stored under your distro’s root (%localappdata\lxss
Therefore, if you use a Windows app/tool/console to create and/or modify a file under your distro root, it won’t have any Linux file metadata (e.g. permissions, owner, timestamps, etc.) stored in its extended attributes.
Also, several Windows tools implement different strategies to handle file updating and don’t actually edit files in-place. When apps/tools save changes to a file, the original files are often deleted and re-created, or cloned edited and then copied over the original file, etc. During such operations, NT file extended properties are often not persisted and are “lost”.
Thus, if a file has no/incorrect Linux file metadata, WSL may not be able to correctly open/read/write the file, and will likely cause apps to assume the file is corrupt … or even overwrite file contents actually corrupting data!
So what SHOULD I do?
To work on files using both Windows and Linux tools, store & work on those files in your Windows filesystem, and access them from both Windows and from Bash via /mnt/<drive>/path (e.g. /mnt/c/dev/project/...)
When you access files on your Windows filesystem from within Bash, WSL honors the NT filesystem behaviors (e.g. case-insensitivity), permissions, etc. so you can easily access the same files using both Windows tools and Bash tools without having to copy files back and forth between filesystems.
Therefore, be sure to follow these two rules in order to avoid losing files, and/or corrupting your data:
Remember: There’s a reason we gave the %localappdata%\lxss\ folder ‘hidden’ & ‘system’ attributes
- DO store files in your Windows filesystem that you want to create/modify using Windows tools AND Linux tools
- DO NOT create / modify Linux files from Windows apps, tools, scripts or consoles
For more background into how the WSL filesystem infrastructure works, be sure to read/watch this AWESOME blog & video
which explains things in much more detail.
Please help us share this guidance far and wide – tweet, post and blog linking to this post!!